Skip to main content

Handling Authentication

Cloud has the newest features. Some may not be in a release yet; the newest release is Identity & Access Management 7.3.0.

This guide explains how to implement authentication in a Node.js server-side application using the Choreo Identity & Access Management Node.js SDK. You will set up the client, redirect users to the identity provider for sign-in, handle the OAuth 2.0 callback, and implement sign-out.

Handling Authentication​

1. Prerequisites​

  • Node.js 18 or later - An application registered in Choreo Identity & Access Management with the callback URL configured (for example, http://localhost:3000/callback) - The @thunderid/node package installed

2. 1. Initialize the Client​

Create a shared authentication client instance and initialize it with your application's configuration.

src/auth.ts

// auth is an instance of your framework client (e.g. from @thunderid/express)
// initialized with this configuration
const config: ThunderIDNodeConfig = {
clientId: '<your-client-id>',
serverOrigin: 'https://<DATA_PLANE_URL>',
afterSignInUrl: 'http://localhost:3000/callback',
afterSignOutUrl: 'http://localhost:3000',
scope: ['openid', 'profile'],
}

export default config

3. 2. Start the Sign-In Flow​

When the user navigates to the sign-in route, generate a session identifier, store it in a cookie, and redirect the user to the identity provider's authorization endpoint.

src/routes/sign-in.ts
CookieConfig,
generateSessionId,
getSessionCookieOptions,
} from '@thunderid/node'

app.get('/sign-in', async (req, res) => {
let sessionId = req.cookies[CookieConfig.SESSION_COOKIE_NAME]

if (!sessionId) {
sessionId = generateSessionId()
res.cookie(
CookieConfig.SESSION_COOKIE_NAME,
sessionId,
getSessionCookieOptions({ maxAge: 3600 }),
)
}

await auth.signIn(
(authUrl) => res.redirect(authUrl),
sessionId,
)
})

4. 3. Handle the Callback​

After the user authenticates at the identity provider, the browser redirects to the callback URL with the authorization code and state parameter. Validate state, then exchange the authorization code for tokens.

src/routes/callback.ts

app.get('/callback', async (req, res) => {
const sessionId = req.cookies[CookieConfig.SESSION_COOKIE_NAME]

if (!sessionId) {
return res.redirect('/sign-in')
}

const { code, state } = req.query

await auth.signIn(
(authUrl) => res.redirect(authUrl),
sessionId,
code as string,
state as string,
)

// Refresh the cookie with the full session expiry time
res.cookie(
CookieConfig.SESSION_COOKIE_NAME,
sessionId,
getSessionCookieOptions({ maxAge: 3600 }),
)

res.redirect('/dashboard')
})

5. 4. Implement Sign-Out​

To sign a user out, clear the session data and redirect the user to the identity provider's end session endpoint.

src/routes/sign-out.ts

app.get('/sign-out', async (req, res) => {
const sessionId = req.cookies[CookieConfig.SESSION_COOKIE_NAME]

if (!sessionId) {
return res.redirect('/')
}

const signOutUrl = await auth.signOut(sessionId)

res.clearCookie(CookieConfig.SESSION_COOKIE_NAME)
res.redirect(signOutUrl)
})

6. Next Steps​

Was this page helpful?