In my Next.js project, I have successfully integrated next-auth and now have access to a JWT token and session object:
export const { signIn, signOut, auth } = NextAuth({
...authConfig,
providers: [
CredentialsProvider({
async authorize(credentials) {
try {
const user = await login(credentials);
return user;
} catch (error) {
return null;
}
},
}),
],
callbacks: {
async jwt({ token, user }: { token: any; user: any }) {
if (user) {
token.id = (user as CustomUser).id;
token.username = (user as CustomUser).username;
token.img = (user as CustomUser).img;
token.isAdmin = (user as CustomUser).isAdmin;
}
return token;
},
async session({ session, token }: { session: any; token: any }) {
if (token) {
session.user = {
name: token.username,
image: token.img,
id: token.id,
isAdmin: token.isAdmin,
};
}
return session;
},
},
});
To make use of the isAdmin
property in my code, I'm also including it in the session:
const session = await auth();
console.log(session?.user?.isAdmin)
However, I encounter the following error message:
Property 'isAdmin' does not exist on type '{ name?: string | null | undefined; email?: string | null | undefined; image?: string | null | undefined; }'.ts(2339)
Does anyone know how to add more data to the session object?
Here is an example of the user object:
{
_id: new ObjectId('random'),
username: 'admin',
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d3c393034331d1d3a303c3431733e3230">[email protected]</a>',
password: '',
isAdmin: true,
isActive: true,
phone: '',
address: 'no ',
}