refactor: migrate authentication to NextAuth and clean up related services

This commit is contained in:
Julien Froidefond
2025-10-12 15:45:09 +02:00
parent 117ac243f5
commit 7d12a66c12
25 changed files with 558 additions and 353 deletions

View File

@@ -1,44 +1,28 @@
import { NextRequest, NextResponse } from "next/server";
import { auth } from "@/auth";
import { NextResponse } from "next/server";
const COOKIE_NAME = "session_token";
export default auth((req) => {
const { pathname } = req.nextUrl;
const isLoggedIn = !!req.auth;
const isOnLoginPage = pathname === "/login";
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Pages qui ne nécessitent pas d'authentification
const publicPaths = ["/login"];
// Pages API qui ne nécessitent pas d'authentification
const publicApiPaths = ["/api/teams", "/api/auth"];
console.log(pathname);
// Vérifier si c'est une route publique
if (
publicPaths.includes(pathname) ||
publicApiPaths.some((path) => pathname.startsWith(path))
) {
// Pages publiques (API auth et teams)
if (pathname.startsWith("/api/auth") || pathname.startsWith("/api/teams")) {
return NextResponse.next();
}
// Vérifier si c'est un fichier statique
if (
pathname.includes("/_next/") ||
pathname.includes("/favicon.ico") ||
pathname.includes("/public/")
) {
return NextResponse.next();
// Si connecté et sur login, rediriger vers home
if (isLoggedIn && isOnLoginPage) {
return NextResponse.redirect(new URL("/", req.url));
}
// Vérifier le cookie d'authentification (maintenant un UUID)
const userUuid = request.cookies.get(COOKIE_NAME)?.value;
if (!userUuid) {
// Rediriger vers la page de login si pas authentifié
const loginUrl = new URL("/login", request.url);
return NextResponse.redirect(loginUrl);
// Si non connecté et pas sur login, rediriger vers login
if (!isLoggedIn && !isOnLoginPage) {
return NextResponse.redirect(new URL("/login", req.url));
}
return NextResponse.next();
}
});
export const config = {
matcher: [