-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
103 lines (91 loc) · 2.76 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { authMiddleware, redirectToLogin } from 'next-firebase-auth-edge';
import { GetTokensOptions } from 'next-firebase-auth-edge/lib/next/tokens';
import { NextRequest, NextResponse } from 'next/server';
import { QUERY_PARAM_NAME } from './src/constants/Query';
const {
NODE_ENV,
NEXT_PUBLIC_FIREBASE_API_KEY,
NEXT_PUBLIC_FIREBASE_PROJECT_ID,
FIREBASE_CLIENT_EMAIL,
FIREBASE_PRIVATE_KEY,
} = process.env;
if (
!NEXT_PUBLIC_FIREBASE_API_KEY ||
!NEXT_PUBLIC_FIREBASE_PROJECT_ID ||
!FIREBASE_CLIENT_EMAIL ||
!FIREBASE_PRIVATE_KEY
) {
throw new Error('One or more environment variables are undefined.');
}
const COOKIE_NAME = 'AuthToken';
const PROTECTED_PATHS = [
'/dashboard',
'/api/login',
'/api/logout',
'/api/entities',
];
export const config = {
matcher: ['/:path*'],
};
export const options: GetTokensOptions = {
apiKey: NEXT_PUBLIC_FIREBASE_API_KEY!,
cookieName: COOKIE_NAME,
serviceAccount: {
projectId: NEXT_PUBLIC_FIREBASE_PROJECT_ID!,
clientEmail: FIREBASE_CLIENT_EMAIL!,
privateKey: FIREBASE_PRIVATE_KEY?.replace(/\\n/g, '\n') ?? '',
},
cookieSignatureKeys: ['secret1', 'secret2'],
};
export async function middleware(request: NextRequest) {
const allowedEmails = process.env.ALLOWED_EMAILS?.split(',') || [];
// Store current request url in a custom header, which you can read later
const requestHeaders = new Headers(request.headers);
const currentURL = request.url;
const currentPath = new URL(currentURL).pathname;
requestHeaders.set('x-path', currentPath);
// Public middleware.
if (!PROTECTED_PATHS.includes(currentPath)) {
return NextResponse.next({
request: {
headers: requestHeaders,
},
});
}
// Private middleware.
return authMiddleware(request, {
...options,
loginPath: '/api/login',
logoutPath: '/api/logout',
// debug: NODE_ENV !== 'production',
cookieSerializeOptions: {
path: '/',
httpOnly: true,
secure: NODE_ENV === 'production', // Set this to true on HTTPS environments
sameSite: 'lax' as const,
maxAge: 12 * 60 * 60 * 24, // twelve days
},
handleValidToken: async ({ decodedToken: { email } }, headers) => {
if (!email || !allowedEmails.includes(email)) {
console.error('Unauthorized access', { email });
return redirectToLogin(request, {
path: '/login',
publicPaths: ['/login'],
redirectParamKeyName: QUERY_PARAM_NAME,
});
}
return NextResponse.next({
request: {
headers,
},
});
},
handleInvalidToken: async (reason) => {
console.error('Missing or malformed credentials', { reason });
return redirectToLogin(request, {
path: '/login',
publicPaths: ['/login'],
});
},
});
}