-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
59 lines (42 loc) · 1.78 KB
/
middleware.ts
File metadata and controls
59 lines (42 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// import { auth } from "@/auth"
import NextAuth from "next-auth";
import authConfig from "./auth.config";
import { apiAuthPrefix, authRoutes, DEFAULT_LOGIN_REDIRECT, publicRoutes } from "./route";
const {auth}=NextAuth(authConfig);
export default auth((req) => {
// const isLoggedIn= !!req.auth // adding !! makes it an boolean variable
// console.log("ROUTE- ",req.nextUrl.pathname);
// console.log("Is Logged in ",isLoggedIn)
const {nextUrl}=req;
const isLoggedin= !!req.auth;
const isApiAuthRoute=nextUrl.pathname.startsWith(apiAuthPrefix)
const isPublicRoute=publicRoutes.includes(nextUrl.pathname); // checking weather the url is among the publicRoutes we will always allow this public routes
const isAuthRoute = authRoutes.includes(nextUrl.pathname);
if(isApiAuthRoute)
{
return undefined
// auth - next auth routes always allow them
}
if(isAuthRoute){
if(isLoggedin){
return Response.redirect(new URL(DEFAULT_LOGIN_REDIRECT,nextUrl))
}
return undefined;
}
if(!isLoggedin && !isPublicRoute){
// not logged in and not on a public route we will redirect the person to login
return Response.redirect(new URL('/auth/login',nextUrl));
}
return undefined;
})
// we will be using the route path + isLoggedin Paramater to determine what has to be done with
// the Current Route on which the user in RN
// Optionally, don't invoke Middleware on some paths
export const config = {
matcher: [
// Skip Next.js internals and all static files, unless found in search params
'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
// Always run for API routes
'/(api|trpc)(.*)',
],
}