I am trying to build a simple credentials sign flow using next-auth ^4.24.5 with nextjs 14. My user model has a boolean prop called ‘isAdmin’ that I would like to expose out to my session using a jwt strategy.
I can see the isAdmin prop as part of the returned user from User.find().
However, I do not see it as part of ANY object returned from any of the callbacks.
This is my config:
import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import User from "../../../../models/User";
export const authOptions = {
secret: process.env.NEXTAUTH_SECRET,
session: {
strategy: "jwt"
},
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
username: {
label: "Username",
type: "text",
placeholder: "[email protected]",
},
password: { label: "Password", type: "password" },
},
async authorize(credentials, req) {
try {
const user = await User.findOne({
email: credentials.username,
}).exec();
if (user) {
const isValid = await user.comparePassword(credentials.password);
if (isValid) return user;
}
return null;
} catch (err) {
console.log(err);
}
},
}),
],
callbacks: {
async signIn({ user, account, profile, email, credentials }) {
return true;
},
async redirect({ url, baseUrl }) {
return baseUrl;
},
async jwt({ token, user, account, profile, isNewUser }) {
return token;
},
async session({ session, user, token }) {
return session;
},
},
pages: {
signIn: "/auth/signin",
signOut: "/auth/signout",
error: "/auth/error", // Error code passed in query string as ?error=
verifyRequest: "/auth/verify-request", // (used for check email message)
newUser: "/auth/register", // New users will be directed here on first sign in (leave the property out if not of interest)
},
};
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };