Next.js Discord

Discord Forum

Next Auth configuration

Unanswered
Bee posted this in #help-forum
Open in Discord
BeeOP
import type { NextAuthOptions } from "next-auth"; import CredentialsProvider from "next-auth/providers/credentials"; import connectMongoDB from "@/lib/dbConnect"; import UserModel from "@/models/User"; import { userAgent } from "next/server"; export const options: NextAuthOptions = { providers: [ CredentialsProvider({ name: "Credentials", credentials: { email: { label: "Email:", type: "email", placeholder: "your-cool-email", }, password: { label: "Password:", type: "password", placeholder: "your-awesome-password", }, }, async authorize(credentials) { await connectMongoDB(); const user = await UserModel.findOne({ email: credentials?.email }); if ( credentials?.email === user.email && credentials?.password == user.password ) { return user; } else { return null; } }, }), ], callbacks: { // Ref: https://authjs.dev/guides/basics/role-based-access-control#persisting-the-role async jwt({ token, user }) { if (user) { token.name = user.name token.role = user.role } console.log(token); return token }, // If you want to use the role in client components async session({ session, token }) { if (session?.user) { session.user.role = token.role // session.user.name = user.name } return session }, } };
With this code the console log is giving me this
{ email: 'arham@gmail.com', sub: '650196010bc051df6d9875ac', role: 'marketing-employee', iat: 1695276246, exp: 1697868246, jti: '54ce056f-9aee-4423-bca1-e93937c3b1ac' }
Why is there no name in the token

0 Replies