Currently, I am in the process of creating a straightforward credentials sign flow using next-auth ^4.24.5 with a nextjs 14 app. Within my user model, there is a boolean property named 'isAdmin' that I wish to make accessible in my session using a jwt strategy.
Upon inspecting the returned user from User.find(), I can confirm that the isAdmin property is present.
However, I have noticed that it is not included in any object returned from the callbacks.
Below is my configuration setup:
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: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="38524b55514c50785c5755595156165b5755">[email protected]</a>",
},
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 };
If anyone can identify the issue and provide insight, I would greatly appreciate it. Thank you in advance!