Skip to content

Commit

Permalink
feat: middleware
Browse files Browse the repository at this point in the history
  • 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.
36 changes: 36 additions & 0 deletions src/middleware.ts
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
};

0 comments on commit 6fb43a1

Please sign in to comment.