ok so now i have solved this i was using wrong sub
my next question is the accesstoken will expire in some hours or mins
so we need to refresh it every time with the refresh token (which we are storing already)
We already store the refresh token, and I need to implement a solution in HTML and Vanilla JavaScript to automatically refresh the access token whenever it expires.
Could you please help me with a code example that demonstrates how to:
- Use the stored refresh token to obtain a new access token.
- Apply this new access token to bypass the login request.
here is my current code
<script type="module">
import { CustomIdentityRequestWrapper } from './custom.js';
const schedulerEditor = document.querySelector("nylas-scheduler-editor");
const accessToken = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const domain ='https://api.eu.nylas.com/v3'; // or 'https://api.us.nylas.com/v3' for EU data center
const nylasApiRequest = new CustomIdentityRequestWrapper(accessToken, domain);
schedulerEditor.nylasApiRequest = nylasApiRequest
schedulerEditor.schedulerPreviewLink = `${window.location.origin}/?config_id={config.id}`; // Replace with the URL of the page where you want to display the scheduler
schedulerEditor.defaultSchedulerConfigState = {
selectedConfiguration: {
requires_session_auth: false, // creates public configuration which does not require session tokens
scheduler: {
// organizer_confirmation_url: `${window.location.origin}/confirmation/:booking_ref`,
cancellation_url: `${window.location.origin}/cancel/:booking_ref`,
rescheduling_url: `${window.location.origin}/reschedule/:booking_ref`,
// confirmation_redirect_url: `${window.location.origin}/booking-confirmed`
},
},
};
</script>
here is custom.js file code
export class CustomIdentityRequestWrapper {
accessToken;
domain;
constructor(accessToken, domain) {
// Initialize the class
this.accessToken = accessToken;
this.domain = domain;
}
async request(args) {
try {
const response = await fetch(`${this.domain}/grants/me/${args.path}`, {
method: args.method,
body: JSON.stringify(args.body),
headers: {
...args.headers,
'Authorization': `Bearer ${this.accessToken}`,
'Content-Type': 'application/json',
},
});
// Check if the response is not okay (e.g., 404, 500)
if (!response.ok) {
console.error(`Error: ${response.status} ${response.statusText}`);
return { error: `Error: ${response.status} ${response.statusText}` };
}
// Parse the response
const data = await response.json();
return [data, null];
} catch (error) {
console.error('Fetch error:', error);
return { error: "Error" };
}
}
/**
* This method returns the current user's information.
*/
async currentUser() {
// IMPLEMENT: Get the logged in user's ID token and return the user information
return {
id: '7dXXXXXXXXXXXXXXXXXX',
email: 'dhruv.....................',
name: 'dhruv',
provider: 'google',
};
}
/**
* This method sets the default authentication arguments to use when authenticating the user.
*/
async setDefaultAuthArgs(authArgs) {
// Set the default authentication arguments
return authArgs;
}
/**
* This method returns the URL to redirect the user to for authentication.
*/
async authenticationUrl() {
// IMPLEMENT: Return the URL to redirect the user to for authentication
return 'http://localhost:3000/scheduler-editor';
}
}
and also confirm that the sub used here in above code is same everytime right it doesn’t change (which we get from “https://api.eu.nylas.com/v3/connect/tokeninfo?id_token=my_id_token” after succesful auth)