Created
November 28, 2021 14:38
-
-
Save nmchenry01/a9dbf243feaa09eec8c95c1f0bc32e86 to your computer and use it in GitHub Desktop.
An example of an Auth0 custom action that appends a tenantId to a user on login
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Handler that will be called during the execution of a PostLogin flow. | |
* | |
* @param {Event} event - Details about the user and the context in which they are logging in. | |
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login. | |
*/ | |
exports.onExecutePostLogin = async (event, api) => { | |
const redirectURI = event.request.query.redirect_uri; | |
const appMetadata = event.user.app_metadata; | |
// Check if tenant_id already exists app_metadata object (function implementation omitted) | |
const tenantIdExists = checkIfTenantIdAlreadyExists(appMetadata) | |
if (tenantIdExists) | |
{ | |
// If tenant_id exists, add app_metadata to access token and return | |
api.accessToken.setCustomClaim("https://api.myapplicationdomain.com/tenant", appMetadata); | |
return; | |
} | |
// Extract tenant name from the redirect URI (function implementation omitted) | |
const tenantName = parseTenantName(redirectURI); | |
// Retrieve the tenant_id from your remote DB (function implementation omitted) | |
const tenantId = await getTenantId(tenantName) | |
// If the tenant doesn't exist for whatever reason, deny access | |
if (!tenantId) | |
{ | |
api.access.deny( | |
`Tenant with name ${tenantName} not found` | |
); | |
} | |
// Set tenant_id in app_metadata, and then add to access token | |
api.user.setAppMetadata("tenant_id", id); | |
api.accessToken.setCustomClaim("https://api.myapplicationdomain.com/tenant", appMetadata); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment