add: new property on session & token

This commit is contained in:
Elie Baier 2023-12-20 19:36:35 +01:00
parent 2e2a44cb5d
commit 8b32c62754
2 changed files with 25 additions and 19 deletions

View File

@ -9,7 +9,8 @@ export async function authenticate(key: string) {
id: true,
firstName: true,
lastName: true,
isTeacher: true
isTeacher: true,
isAdmin: true
},
where: {
key: key
@ -26,24 +27,26 @@ export const authOptions: NextAuthOptions = {
strategy: "jwt",
},
callbacks: {
async jwt({ token, user }) {
if (user) {
token.userId = parseInt(user.id as string);
token.firstName = user.firstName;
token.lastName = user.lastName;
token.isTeacher = user.isTeacher;
}
return token;
},
async session({ session, token, user }) {
if(token) {
session.user.id = token.userId;
session.user.firstName = token.firstName;
session.user.lastName = token.lastName;
session.user.isTeacher = token.isTeacher;
}
return session;
},
async jwt({ token, user }) {
if (user) {
token.userId = parseInt(user.id as string);
token.firstName = user.firstName;
token.lastName = user.lastName;
token.isTeacher = user.isTeacher;
token.isAdmin = user.isAdmin;
}
return token;
},
async session({ session, token, user }) {
if(token) {
session.user.id = token.userId;
session.user.firstName = token.firstName;
session.user.lastName = token.lastName;
session.user.isTeacher = token.isTeacher;
session.user.isAdmin = token.isAdmin;
}
return session;
},
},
pages: {
signIn: '/',

View File

@ -8,6 +8,7 @@ declare module "next-auth" {
firstName: string;
lastName: string
isTeacher: boolean;
isAdmin: boolean;
} & DefaultSession["user"];
}
interface User {
@ -15,6 +16,7 @@ declare module "next-auth" {
firstName: string;
lastName: string
isTeacher: boolean;
isAdmin: boolean;
}
}
@ -24,5 +26,6 @@ declare module "next-auth/jwt" {
firstName: string;
lastName: string
isTeacher: boolean;
isAdmin: boolean;
}
}