-
Notifications
You must be signed in to change notification settings - Fork 39
/
middleware.ts
73 lines (70 loc) · 2.25 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
import { NextRequest, NextResponse } from 'next/server';
import {
AUTH_LOGIN_CALLBACK_PARAM,
AUTH_LOGIN_PATHNAME,
PROMPT_QUERY_PARAM,
REDIRECT_FROM_WIX_LOGIN_STATUS,
WIX_REFRESH_TOKEN,
} from '@app/model/auth/auth.const';
import { getServerWixClient } from '@app/model/auth/wix-client.server';
import type { WixClientType } from '@app/model/auth/wix-client.base';
const setVisitorTokens = async ({
wixClient,
response,
}: {
wixClient: WixClientType;
request: NextRequest;
response: NextResponse;
}) => {
const tokens = await wixClient!.auth.generateVisitorTokens();
response.cookies.set(WIX_REFRESH_TOKEN, JSON.stringify(tokens.refreshToken), {
maxAge: 60 * 60 * 24 * 30,
});
};
export async function middleware(request: NextRequest) {
const cookies = request.cookies;
const requestHeaders = new Headers(request.headers);
const requestUrl = request.url;
requestHeaders.set('x-middleware-request-url', requestUrl);
const res = NextResponse.next({
request: {
headers: requestHeaders,
},
});
const wixClient = getServerWixClient({
cookieStore: request.cookies,
});
const isLoggedIn = wixClient?.auth.loggedIn();
if (!cookies.get(WIX_REFRESH_TOKEN) && !isLoggedIn) {
await setVisitorTokens({ response: res, wixClient, request });
}
const wixMemberLoggedIn = request.nextUrl.searchParams.get(
REDIRECT_FROM_WIX_LOGIN_STATUS
);
if (wixMemberLoggedIn === 'false' && isLoggedIn) {
cookies.delete(WIX_REFRESH_TOKEN);
await setVisitorTokens({ response: res, wixClient, request });
}
if (
wixMemberLoggedIn === 'true' ||
(!isLoggedIn && request.nextUrl.pathname.startsWith('/account'))
) {
const redirectUrl = new URL(AUTH_LOGIN_PATHNAME, requestUrl);
const loginCallbackUrl = new URL(requestUrl);
redirectUrl.searchParams.delete(REDIRECT_FROM_WIX_LOGIN_STATUS);
loginCallbackUrl.searchParams.delete(REDIRECT_FROM_WIX_LOGIN_STATUS);
redirectUrl.searchParams.set(
AUTH_LOGIN_CALLBACK_PARAM,
loginCallbackUrl.toString()
);
redirectUrl.searchParams.set(PROMPT_QUERY_PARAM, 'none');
return NextResponse.redirect(redirectUrl);
}
return res;
}
export const config = {
unstable_allowDynamic: [
'**/node_modules/lodash/**',
'**/node_modules/@wix/**',
],
};