refactor: migrate authentication to NextAuth and clean up related services
This commit is contained in:
@@ -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: [
|
||||
|
||||
Reference in New Issue
Block a user