Skip to content Skip to sidebar Skip to footer

Get AccessToken In Auth0

I am using auth0 and nextJS. I want to do next: When the user will add his credentials and will log in he is redirected to the callback API. And here import auth0 from '../../u

Solution 1:

Using v1.2.0 of the nextjs-auth0 library, you can access the identity token during the callback handler.

import { handleAuth, handleLogin, handleCallback } from '@auth0/nextjs-auth0';

const afterCallback = (req, res, session, state) => {
    console.log(session.idToken);
    if (!session.user.isAdmin) {
        throw new UnauthorizedError('User is not admin');
    }
    return session;
}

export default handleAuth({
    async callback(req, res) {
        try {
            await handleCallback(req, res, { afterCallback });
        } catch (error) {
            res.status(error.status || 500).end(error.message);
        }
    }
});

session variable afterCallback

However, keep in mind, you should generally avoid looking inside the access token by the client application. If you need to relay user information to the client, you should place it in an id_token. The access token is for use by the API, and your client application should not take any dependency on its content format or semantics since access tokens by design have no defined format.


Post a Comment for "Get AccessToken In Auth0"