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';
constafterCallback = (req, res, session, state) => {
console.log(session.idToken);
if (!session.user.isAdmin) {
thrownewUnauthorizedError('User is not admin');
}
return session;
}
exportdefaulthandleAuth({
asynccallback(req, res) {
try {
awaithandleCallback(req, res, { afterCallback });
} catch (error) {
res.status(error.status || 500).end(error.message);
}
}
});
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"