Welcome to the Kinde community.

A
A
A
M
T

Verifying audience in access token in Node.js API

Show of example of verifying the audience in the access token in Node.js API

K
1 comment

To verify the audience in the access token in a Node.js API, you can use the Kinde SDK. Below is an example of how to do this: 1. *Install the Kinde SDK*: ``` npm install @kinde/jwt-validator ``` 2. *Set up the Kinde client with the audience*: ``` const { KindeClient } = require('@kinde/jwt-validator'); const options = { audience: '', // Replace with your actual audience // other options }; const kindeClient = new KindeClient(options); ``` 3. *Verify the audience in the access token*: ``` const express = require('express'); const app = express(); app.use(async (req, res, next) => { try { const token = req.headers.authorization.split(' ')[1]; const isValid = await kindeClient.verifyToken(token); if (isValid) { const audience = kindeClient.getClaim(req, 'aud'); if (audience.includes('')) { next(); } else { res.status(403).send('Invalid audience'); } } else { res.status(401).send('Invalid token'); } } catch (error) { res.status(500).send('Internal Server Error'); } }); app.get('/protected', (req, res) => { res.send('This is a protected route'); }); app.listen(3000, () => { console.log('Server is running on port 3000'); }); ``` In this example, the middleware checks the validity of the token and verifies that the audience claim (`aud`) matches the expected audience (``). If the token is valid and the audience matches, the request proceeds to the next middleware or route handler. Otherwise, it responds with an appropriate error message.

Add a reply
Sign up and join the conversation on Slack
Join