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

View File

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