-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
alexandre-kakal-akarah
committed
Jun 18, 2024
1 parent
1c39109
commit 6fb43a1
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { withAuth } from "next-auth/middleware"; | ||
import { NextResponse } from "next/server"; | ||
|
||
export default withAuth( | ||
function middleware(req) { | ||
// Validation des données ? | ||
// ajouter les informations de la session à la requête ? | ||
return NextResponse.next(); | ||
}, | ||
{ | ||
callbacks: { | ||
authorized: ({ token, req }) => { | ||
const route = req.nextUrl.pathname; | ||
console.log("middleware route", route, "methode", req.method); | ||
console.log("middleware token callback", token); | ||
|
||
// routes qui ne requièrent pas d'authentification | ||
if (!token) { | ||
if (!route.startsWith("/api/users") && req.method === "GET") { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
// si token et role !admin => return false pour les routes admins (/api/users en GET) | ||
|
||
return true; | ||
}, | ||
}, | ||
}, | ||
); | ||
|
||
export const config = { | ||
matcher: ["/api/:path*"], // Appliquer le middleware à toutes les routes sous /api | ||
}; |