-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware.ts
49 lines (43 loc) · 1.16 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 { NextResponse } from 'next/server';
import withAuth from 'next-auth/middleware';
const isProtectedUrl = (req: Request, protectedUrls: string[]) => {
return protectedUrls.some((url) => req.url.includes(url));
};
const protectedUrls = ['/dashboard', '/projects'];
export default withAuth(
(req) => {
if (isProtectedUrl(req, protectedUrls)) {
if (!req.nextauth.token) {
return NextResponse.redirect(new URL('/login', req.url));
}
if (!req.nextauth.token.organizationId) {
return NextResponse.redirect(new URL('/organization', req.url));
}
}
},
{
pages: {
signIn: '/login',
},
callbacks: {
authorized: ({ req, token }) => {
if (isProtectedUrl(req, protectedUrls) && !token) {
return false;
}
return true;
},
},
},
);
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
'/((?!api|_next/static|_next/image|favicon.ico).*)',
],
};