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:
73
lib/authenticate.ts
Normal file
73
lib/authenticate.ts
Normal 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);
|
||||
Reference in New Issue
Block a user