1
0
mirror of https://github.com/Fayorg/calendrier-avant.git synced 2026-05-27 17:18:38 +02:00

add: using nextauth WIP

This commit is contained in:
2023-12-15 15:21:51 +01:00
parent 5f049aad5b
commit 2eaccd52e4
7 changed files with 255 additions and 17 deletions

73
lib/authenticate.ts Normal file
View File

@@ -0,0 +1,73 @@
import Credentials from "next-auth/providers/credentials";
import prisma from "./prisma";
import { getServerSession, RequestInternal, type NextAuthOptions, User } from "next-auth";
export async function authenticate(key: string) {
console.log("Running authenticate function with key: " + key);
const user = await prisma.users.findUnique({
select: {
id: true,
firstName: true,
lastName: true,
isTeacher: true
},
where: {
key: key
}
});
console.log("Found user: " + JSON.stringify(user));
if(!user) return null;
return user;
}
export const authOptions: NextAuthOptions = {
session: {
strategy: "jwt",
},
callbacks: {
async jwt({ token, user }) {
if (user) {
token.userId = user.id;
token.firstName = user.firstName;
token.lastName = user.lastName;
token.isTeacher = user.isTeacher;
}
return token;
},
async session({ session, token, user }) {
session.user.id = token.userId;
session.user.firstName = token.firstName;
session.user.lastName = token.lastName;
session.user.isTeacher = token.isTeacher;
return session;
},
},
pages: {
signIn: '/',
},
providers: [
Credentials({
name: "Credentials",
credentials: {
key: { label: "Key", type: "password" }
},
async authorize(credentials: Record<"key", string> | undefined, req: Pick<RequestInternal, "body" | "query" | "headers" | "method">) {
const { key } = credentials as {
key: string
};
console.log("Running authorize function with key: " + key)
const user = await authenticate(key) as User | null
console.log("Found user in authorize: " + JSON.stringify(user));
return user;
}
})
],
};
export const getAuthServerSession = () => getServerSession(authOptions);