Configure authentication for the Scheduler Editor Component
The Scheduler Editor Component uses the Hosted authentication details in nylasSessionsConfig to interact with the Nylas APIs. Instead of using nylasSessionsConfig, you can use nylasApiRequest property in the either of the following situations:
- You’re already using Nylas Hosted auth in your application.
- You want to use Custom auth or a different HTTP client library for authentication.
Download Nylas identity package
To use nylasApiRequest, you first need to download the Nylas identity package:
npm i @nylas/identity@latestUse existing Nylas Hosted authentication
If you’re already using Nylas Hosted auth in your application, you can use nylasApiRequest with NylasIdentityRequestWrapper.
!!!include(v3_code_samples/scheduler/custom-auth-nylas-identity-request-wrapper.html" />!!!include(v3_code_samples/scheduler/custom-auth-nylas-identity-request-wrapper.tsx)!!!Use Custom authentication
If you want to use Custom auth or a different HTTP client library for authentication, you can define a custom wrapper (CustomIdentityRequestWrapper) which implements nylasApiRequest, and set the nylasApiRequest property with CustomIdentityRequestWrapper.
The Scheduler Editor Component and CustomIdentityRequestWrapper require an access token from the same origin. To get the access token:
- Add a callback URI.
- Make an authorization request to retrieve a code.
- Exchange the code for an access token.
Add a callback URI
- In your Nylas application, click Hosted Authentication in the left navigation, and click Callback URIs.
- Click Add a callback URI.
- Select the JavaScript platform.
- For URL, enter
https://127.0.0.1:3000/scheduler-editor. This assumes you host the Scheduler Editor onlocalhost:3000. The URL will vary based on your hosting choice. - For Origin, enter
https://127.0.0.1:3000. - Click Add callback URI.
Make an authorization request
Make a GET /v3/connect/auth request to retrieve an authorization code that you can exchange for an access token.
/v3/connect/auth? client_id=<NYLAS_CLIENT_ID> &redirect_uri=https://127.0.0.1:3000/scheduler-editor &response_type=code &provider=google &access_type=offlineExchange the code for an access token
Using the code from your authorization request, make a POST /v3/connect/token request to get the access token.
curl --request POST \ --url 'https://api.us.nylas.com/v3/connect/token' \ --header 'Content-Type: application/json' \ --header 'Origin: 'https://127.0.0.1:3000' \ --data '{ "code": "<AUTHORIZATION_CODE>", "client_id": "<NYLAS_CLIENT_ID>", "client_secret": "<NYLAS_API_KEY>", "redirect_uri": "https://127.0.0.1:3000/scheduler-editor", "grant_type": "authorization_code" }'You can then use the access token in CustomIdentityRequestWrapper and the Scheduler Editor Component. Make sure that CustomIdentityRequestWrapper has a currentUser function that returns the same email address that is associated with the access token.
Example: CustomIdentityRequestWrapper and the Scheduler Editor Component
!!!include(v3_code_samples/scheduler/custom-identity-request-wrapper-example.js)!!!!!!include(v3_code_samples/scheduler/custom-auth-custom-identity-request-wrapper.html" />!!!include(v3_code_samples/scheduler/custom-auth-custom-identity-request-wrapper.tsx)!!!