forked from boxyhq/saas-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
45 lines (37 loc) · 1.07 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
import micromatch from 'micromatch';
import { getToken } from 'next-auth/jwt';
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
// Add routes that don't require authentication
const unAuthenticatedRoutes = [
'/api/hello',
'/api/health',
'/api/auth/**',
'/api/oauth/**',
'/api/scim/v2.0/**',
'/auth/**',
'/invitations/*',
'/api/invitations/*',
'/terms-condition',
];
export default async function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
// Bypass routes that don't require authentication
if (micromatch.isMatch(pathname, unAuthenticatedRoutes)) {
return NextResponse.next();
}
const token = await getToken({
req,
});
// No token, redirect to signin page
if (!token) {
const url = new URL('/auth/login', req.url);
url.searchParams.set('callbackUrl ', encodeURI(req.url));
return NextResponse.redirect(url);
}
// All good, let the request through
return NextResponse.next();
}
export const config = {
matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
};