This method will enable getToken app-wide, with a live copy of dbUser
session: {
strategy: “jwt”
}
Import {getToken} from “next-auth/jwt”;
Const token = await getToken({req})
(Works everywhere; client, API, middleware, etc)
Just ping /api/auth/session to refresh, as of doc:
Requests to /api/auth/signin, /api/auth/session and calls to getSession(), getServerSession(), useSession() will invoke this function, but only if you are using a JWT session. This method is not invoked when you persist sessions in a database.
https://next-auth.js.org/configuration/callbacks
However, in server components, you need to use getServerSession from ‘next-auth’. To enable custom props here, export authOptions and call getServerOptions with authOptions
Notes:
- JWT token always includes the latest dbUser
- JWT token refreshes on CLIENT when pinging /api/auth/session
- Session includes a copy of dbUser (not needed, because we use getToken everywhere)
const authOptions = {
session: {
strategy: "jwt",
},
adapter: PrismaAdapter(prisma),
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
})
],
callbacks: {
async jwt({ token, user, trigger }) {
const dbUser = await prisma.user.findUnique({
where: {
email: token.email
}
})
token.dbUser = dbUser;
return token;
},
async session({ session, user, token }) {
session.dbUser = token.dbUser;
return session;
}
}
}