-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
35 lines (27 loc) · 1.23 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
import {NextRequest, NextResponse} from 'next/server';
// TODO: These routes will need to be updated.
const excludedPaths = ['/login', '/signup', '/email', '/favicon.ico', '/_next', '/api'];
export async function middleware(request: NextRequest) {
const response = NextResponse.next();
const { pathname } = request.nextUrl;
// Check if the path is in the excludedPaths array
if (excludedPaths.some(path => pathname.startsWith(path))) {
// If it is, return the response without checking for tokens
return response;
}
const accessToken = request.cookies.get('dmspt');
const refreshToken = request.cookies.get('dmspr');
/* TODO: might want to add a 'redirect' query param to url to redirect user after
login to the original page they were trying to get to.*/
// Check for tokens for paths that are not excluded from authentication
if (!excludedPaths.some(path => pathname.startsWith(path))) {
if (!accessToken && !refreshToken) {
return NextResponse.redirect(new URL('/login', request.url));
}
}
// Add url info to custom header. Need this for just the /dmps landing page
if (request.nextUrl.pathname.startsWith('/dmps')) {
response.headers.set('x-url', request.nextUrl.href);
}
return response;
}