-
Notifications
You must be signed in to change notification settings - Fork 5
/
middleware.ts
47 lines (42 loc) · 1.27 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
import { NextRequest, NextResponse } from "next/server";
import { verifyAccessToken } from "./utils/cookies";
// const isAPIRoute = (pathname: string) => {
// return pathname.startsWith('/api');
// }
export async function middleware(req: NextRequest) {
console.log("middleware");
// const { pathname } = req.nextUrl;
// if (isAPIRoute(pathname)) {
// return NextResponse.redirect(new URL('/api/auth/unauthorized', req.url));
// }
const accessToken = req.cookies.get("access")?.value;
if (!accessToken) {
return NextResponse.redirect(new URL("/auth/login", req.url));
} else {
try {
const verifiedToken = await verifyAccessToken(accessToken);
// if expired or invalid token redirect to login
if (!verifiedToken) {
return NextResponse.redirect(new URL("/auth/login", req.url));
}
} catch (error) {
console.log(error);
return NextResponse.redirect(new URL("/auth/login", req.url));
}
return NextResponse.next();
}
}
// This function is called for every request that matches the `config.matcher` pattern
export const config = {
matcher: [
"/overview",
"/transactions",
"/analytics",
"/wallet",
"/invoice",
"/accounts",
"/settings",
"/profile",
// "/api/:path*"
],
};