forked from bradtraversy/prostore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.config.ts
46 lines (39 loc) · 1.38 KB
/
auth.config.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
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { NextAuthConfig } from 'next-auth';
import { NextResponse } from 'next/server';
export const authConfig = {
providers: [], // Required by NextAuthConfig type
callbacks: {
authorized({ request, auth }: any) {
// Array of regex patterns of paths we want to protect
const protectedPaths = [
/\/shipping-address/,
/\/payment-method/,
/\/place-order/,
/\/profile/,
/\/user\/(.*)/,
/\/order\/(.*)/,
/\/admin/,
];
// Get pathname from the req URL object
const { pathname } = request.nextUrl;
// Check if user is not authenticated and accessing a protected path
if (!auth && protectedPaths.some((p) => p.test(pathname))) return false;
// Check for session cart cookie
if (!request.cookies.get('sessionCartId')) {
// Generate new session cart id cookie
const sessionCartId = crypto.randomUUID();
// Create new response and add the new headers
const response = NextResponse.next({
request: {
headers: new Headers(request.headers),
},
});
// Set newly generated sessionCartId in the response cookies
response.cookies.set('sessionCartId', sessionCartId);
return response;
}
return true;
},
},
} satisfies NextAuthConfig;