How to add custom attribute to the returned session JWT token? #2952
-
Question 💬In Cognito I'm enhancing the JWT token the AWS provides with my own custom attribute for Hasura. I need to be able to access this attribute from my application once a user is logged in. Currently, when I get the user's session object, it only contains:
Also, if I try to extract the cookie from the browser I get the following:
Is there a way to let next-auth w/Cognito Provider know that I would like to also pull in a custom attribute on the JWT token called "graphql"? How to reproduce ☕️N/A Contributing 🙌🏽No, I am afraid I cannot help regarding this |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Beta Was this translation helpful? Give feedback.
-
In the end it's not much code but it was hard for me to get what the documentation explains without a clear example, so here's one! export default NextAuth({
callbacks: {
async jwt({ account, token, user }) {
if (account && user) {
if (["[email protected]"].includes(user.email)) {
token.role = "admin";
} else {
token.role = "user";
}
}
return token;
},
async session({ session, token }) {
session.user.role = token.role;
return session;
},
},
}); Then, in an API route: export default async function adminRoute(req, res) {
const session = await getSession({ req });
if (session?.user.role !== "admin") {
res.status(401).end();
return;
}
res.json({ hello: "admin" });
} |
Beta Was this translation helpful? Give feedback.
https://next-auth.js.org/configuration/callbacks