-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
42 lines (34 loc) · 1006 Bytes
/
middleware.js
File metadata and controls
42 lines (34 loc) · 1006 Bytes
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
import { NextResponse } from "next/server"
export function middleware(request) {
const path = request.nextUrl.pathname
const isPublicPath =
path === "/login" ||
path === "/signup" ||
path === "/verifyemail" ||
path === "/shared"
let token = ""
if (request.cookies.has("token")) {
token = request.cookies.get("token").value
}
//Temporarily not letting anyone access "/" route
if (path === "/") {
return NextResponse.redirect(new URL("/dashboard", request.nextUrl))
}
if (isPublicPath && token) {
return NextResponse.redirect(new URL("/dashboard", request.nextUrl))
}
if (!isPublicPath && !token) {
return NextResponse.redirect(new URL("/login", request.nextUrl))
}
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - favicon.ico (favicon file)
*/
"/((?!api|_next/static|favicon.ico|shared).*)",
],
}