Files
peakskills/middleware.ts
Julien Froidefond 5c71ce1a54 refactor: update authentication flow and cookie management
- Changed COOKIE_NAME from "peakSkills_userId" to "session_token" for better clarity.
- Updated AuthClient to handle login and registration with new data structures.
- Enhanced AuthWrapper to manage user sessions and display appropriate messages.
- Added error handling in LoginForm and RegisterForm for better user feedback.
- Refactored user service methods to streamline user creation and verification processes.
2025-08-25 16:19:31 +02:00

54 lines
1.4 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
const COOKIE_NAME = "session_token";
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/auth", "/api/teams"];
// Vérifier si c'est une route publique
if (
publicPaths.includes(pathname) ||
publicApiPaths.some((path) => pathname.startsWith(path))
) {
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();
}
// 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);
}
return NextResponse.next();
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
"/((?!_next/static|_next/image|favicon.ico).*)",
],
};