-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
49 lines (41 loc) · 1.62 KB
/
middleware.ts
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
import { NextRequest, NextResponse } from 'next/server';
export async function middleware(req: NextRequest){
const token = req.cookies.get("token")
const currUrl = new URL(req.url)
const isHomePage = currUrl.pathname === "/home"
const isApiRequest = currUrl.pathname.startsWith("/api")
if(token && (
currUrl.pathname.startsWith("/signin") ||
currUrl.pathname.startsWith("/signup") ||
currUrl.pathname === "/" ||
currUrl.pathname.startsWith("/reset-password")
) && !isHomePage){
return NextResponse.redirect(new URL("/home", req.url))
}
if(!token){
if(!(
currUrl.pathname.startsWith("/signin") ||
currUrl.pathname.startsWith("/signup") ||
currUrl.pathname.startsWith("/reset-password")
) && !(
currUrl.pathname.startsWith("/api/videos") ||
currUrl.pathname.startsWith("/api/signup") ||
currUrl.pathname.startsWith("/api/signin") ||
currUrl.pathname.startsWith("/api/reset-password")
)){
return NextResponse.redirect(new URL("/signin", req.url))
}
if(isApiRequest && !(
currUrl.pathname.startsWith("/api/videos") ||
currUrl.pathname.startsWith("/api/signup") ||
currUrl.pathname.startsWith("/api/signin") ||
currUrl.pathname.startsWith("/api/reset-password")
)){
return NextResponse.redirect(new URL("/signin", req.url))
}
}
return NextResponse.next()
}
export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
}