Create client
Create the SDK client and run core auth helpers
Create a ScalekitClient with your environment URL, client ID, and client secret. All other clients hang off this instance.
Store credentials in environment variables. Never hard-code secrets. After you create the client, use Sessions to validate tokens or open a domain client such as Organizations or Users.
validateAccessToken
Section titled “validateAccessToken”#asyncvalidateAccessToken
Validates the access token and returns a boolean result.
The token to be validated.
Optional request settings.
Returns true if the token is valid, false otherwise.
const isValid = await scalekit.validateAccessToken(accessToken);generateClientToken
Section titled “generateClientToken”#asyncgenerateClientToken
Generates an M2M access token using the client credentials grant for the given clientId and clientSecret.
The client ID to authenticate with
The client secret to authenticate with
The access token string
authenticateWithCode
Section titled “authenticateWithCode”#asyncauthenticateWithCode
Exchanges an authorization code for access tokens and user information.
The authorization code received in the callback URL after.
The same redirect URI used in getAuthorizationUrl().
Optional request settings.
Tokens and user claims.
app.get('/auth/callback', async (req, res) => { const { code } = req.query;
const result = await scalekit.authenticateWithCode( code, 'https://yourapp.com/auth/callback' );
req.session.accessToken = result.accessToken; req.session.user = result.user;
res.redirect('/dashboard');});getIdpInitiatedLoginClaims
Section titled “getIdpInitiatedLoginClaims”#asyncgetIdpInitiatedLoginClaims
Extracts and validates claims from an IdP-initiated login token.
The token received in the ‘idp_initiated_login’ query.
Optional request settings.
The connection id.
app.get('/auth/callback', async (req, res) => { const { idp_initiated_login } = req.query;
if (idp_initiated_login) { const claims = await scalekit.getIdpInitiatedLoginClaims(idp_initiated_login);
const authUrl = scalekit.getAuthorizationUrl( 'https://yourapp.com/auth/callback', { connectionId: claims.connection_id, organizationId: claims.organization_id, loginHint: claims.login_hint, ...(claims.relay_state && { state: claims.relay_state }) } );
return res.redirect(authUrl); }});refreshAccessToken
Section titled “refreshAccessToken”#asyncrefreshAccessToken
Obtains a new access token using a refresh token.
The refresh token obtained from a previous authentication
Tokens and claims.
const result = await scalekit.refreshAccessToken(oldRefreshToken);// Store the new tokensawait storeTokens(userId, { accessToken: result.accessToken, refreshToken: result.refreshToken});