feat: integrate NextAuth for authentication, refactor login and registration processes, and enhance middleware for session management
This commit is contained in:
61
src/lib/auth.ts
Normal file
61
src/lib/auth.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import NextAuth from "next-auth";
|
||||
import Credentials from "next-auth/providers/credentials";
|
||||
import { AuthServerService } from "@/lib/services/auth-server.service";
|
||||
|
||||
export const { handlers, auth, signIn, signOut } = NextAuth({
|
||||
providers: [
|
||||
Credentials({
|
||||
name: "credentials",
|
||||
credentials: {
|
||||
email: { label: "Email", type: "email" },
|
||||
password: { label: "Password", type: "password" },
|
||||
remember: { label: "Remember me", type: "checkbox" },
|
||||
},
|
||||
async authorize(credentials) {
|
||||
if (!credentials?.email || !credentials?.password) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const userData = await AuthServerService.loginUser(
|
||||
credentials.email as string,
|
||||
credentials.password as string
|
||||
);
|
||||
|
||||
return {
|
||||
id: userData.id,
|
||||
email: userData.email,
|
||||
roles: userData.roles,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Auth error:", error);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
}),
|
||||
],
|
||||
callbacks: {
|
||||
async jwt({ token, user }) {
|
||||
if (user) {
|
||||
// Convertir le tableau en string pour éviter les problèmes de clonage
|
||||
token.roles = JSON.stringify(user.roles);
|
||||
}
|
||||
return token;
|
||||
},
|
||||
async session({ session, token }) {
|
||||
if (token) {
|
||||
session.user.id = token.sub!;
|
||||
// Reconvertir la string en tableau
|
||||
session.user.roles = JSON.parse(token.roles as string);
|
||||
}
|
||||
return session;
|
||||
},
|
||||
},
|
||||
pages: {
|
||||
signIn: "/login",
|
||||
},
|
||||
session: {
|
||||
strategy: "jwt",
|
||||
},
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
});
|
||||
Reference in New Issue
Block a user