Skip to content
Skip to main content

Integrate Auth0 with @nylas/connect

Last updated:

Auth0 is an enterprise authentication platform that supports social logins, passwordless authentication, MFA, and SSO. This guide shows you how to use Auth0 as your identity provider with @nylas/connect so your users can authenticate with Auth0 and connect their email accounts through Nylas.

You need an Auth0 application and a Nylas application configured to work together.

Before connecting your identity provider, configure the IDP settings in the Nylas Dashboard:

  1. Navigate to your application in the Nylas Dashboard.
  2. Go to Hosted AuthenticationIdentity Providers.
  3. Configure the following settings:
    • Allowed Origins: Add the domains where your application will be hosted (e.g., http://localhost:3000, https://yourapp.com). These origins will be allowed to make requests to Nylas with your IDP tokens.
    • Callback URIs: Add the redirect URIs that Nylas will use after authentication (e.g., http://localhost:3000/auth/callback). These must match the redirectUri configured in your NylasConnect instance.

You can access the Identity Provider settings page directly at:

https://dashboard-v3.nylas.com/applications/<YOUR_APP_ID>/hosted-authentication/idp-settings
  1. In your Auth0 Dashboard, navigate to ApplicationsApplications.
  2. Select your application or create a new Single Page Application.
  3. Configure the following settings:
    • Allowed Callback URLs: Add your application’s callback URLs (e.g., http://localhost:3000, https://yourapp.com)
    • Allowed Web Origins: Add your application’s origins (e.g., http://localhost:3000, https://yourapp.com)
    • Allowed Logout URLs: Add URLs where users can be redirected after logout
npm install @nylas/connect @auth0/auth0-spa-js

Initialize Auth0 and @nylas/connect together. The identityProviderToken callback passes the Auth0 token to @nylas/connect so it can associate the user’s identity with their Nylas grant:

import { NylasConnect } from "@nylas/connect";
import { Auth0Client } from "@auth0/auth0-spa-js";
const auth0 = new Auth0Client({
domain: "<AUTH0_DOMAIN>",
clientId: "<AUTH0_CLIENT_ID>",
authorizationParams: {
redirect_uri: window.location.origin,
},
});
const nylasConnect = new NylasConnect({
clientId: "<NYLAS_CLIENT_ID>",
redirectUri: "http://localhost:3000/auth/callback",
identityProviderToken: async () => {
try {
return await auth0.getTokenSilently();
} catch (error) {
console.error("Failed to get Auth0 token:", error);
return null;
}
},
});
async function loginWithAuth0() {
await auth0.loginWithPopup();
const isAuthenticated = await auth0.isAuthenticated();
if (isAuthenticated) {
const user = await auth0.getUser();
console.log("Authenticated as:", user?.email);
}
}
async function connectEmail() {
try {
const result = await nylasConnect.connect({ method: "popup" });
console.log("Email connected:", result.grantInfo?.email);
} catch (error) {
console.error("Failed to connect email:", error);
}
}
async function logout() {
await nylasConnect.logout();
await auth0.logout({
logoutParams: { returnTo: window.location.origin },
});
}

Once the user has connected a mailbox, call the API with the Auth0 access token you already hold. Take the user ID from auth0.getUser() rather than decoding the JWT yourself; the SDK has already validated it.

async function fetchEmails() {
const token = await auth0.getTokenSilently();
const user = await auth0.getUser();
const response = await fetch(
"https://api.us.nylas.com/v3/grants/me/messages",
{
headers: {
Authorization: `Bearer ${token}`,
"X-Nylas-External-User-Id": user?.sub ?? "",
},
},
);
return await response.json();
}

Two headers do the work. Authorization carries your identity provider’s access token, and X-Nylas-External-User-Id carries the user’s sub claim, which is the same ID your application already stores. Nylas resolves /v3/grants/me to the mailbox linked to that ID, so no grant_id appears in the request.

The same 2 headers work across the Email, Calendar, and Contacts endpoints. Swap /messages for /events or /contacts and nothing else changes. For how the linkage is established, and when to use it instead of storing grant IDs, see use your own user IDs instead of grant IDs.