-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
48 lines (40 loc) · 1.15 KB
/
Copy pathmiddleware.ts
File metadata and controls
48 lines (40 loc) · 1.15 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
"use server";
import { NextResponse } from 'next/server'
import { NextRequest } from 'next/server'
import { validateToken } from './services/AuthService';
export async function middleware(request: NextRequest) {
// console.log(request.cookies);
// console.log(request.url);
// console.log(request.nextUrl.pathname);
const pathname = request.nextUrl.pathname;
const token = request.cookies.get("token");
if (token) {
const isTokenValid = (await validateToken(token.value)).data;
console.log(isTokenValid);
if (isTokenValid) {
if (pathname.startsWith("/login") || pathname.startsWith("/signup")) {
return NextResponse.redirect(new URL("/", request.url));
}
else return NextResponse.next();
}
else {
let response = NextResponse.redirect(new URL("/login", request.url));
response.cookies.delete("token");
return response;
}
}
else {
if (pathname.startsWith("/login") || pathname.startsWith("/signup")) {
return NextResponse.next();
}
else return NextResponse.redirect(new URL("/login", request.url));
}
}
export const config = {
matcher: [
"/login",
"/signup",
"/projects/new",
"/projects/:id/edit",
],
}