-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
52 lines (50 loc) · 1.63 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
import { NextRequest, NextResponse } from 'next/server';
import {
AUTH_LOGIN_CALLBACK_PARAM,
AUTH_LOGIN_PATHNAME,
REDIRECT_FROM_WIX_LOGIN_STATUS,
WIX_MEMBER_TOKEN,
WIX_REFRESH_TOKEN,
} from '@app/model/auth/auth.const';
import { getServerWixClient } from '@app/model/auth/wix-client.server';
export async function middleware(request: NextRequest) {
const cookies = request.cookies;
const res = NextResponse.next();
const memberToken = cookies.get(WIX_MEMBER_TOKEN);
const wixClient = getServerWixClient({
cookieStore: request.cookies,
});
if (!cookies.get(WIX_REFRESH_TOKEN) && !memberToken) {
const tokens = await wixClient!.auth.generateVisitorTokens();
res.cookies.set(WIX_REFRESH_TOKEN, JSON.stringify(tokens.refreshToken), {
maxAge: 60 * 60 * 24 * 30,
});
}
const wixMemberLoggedIn = request.nextUrl.searchParams.get(
REDIRECT_FROM_WIX_LOGIN_STATUS
);
if (wixMemberLoggedIn === 'false') {
cookies.delete(WIX_MEMBER_TOKEN);
}
if (
wixMemberLoggedIn === 'true' ||
(!memberToken && request.nextUrl.pathname.startsWith('/account'))
) {
const redirectUrl = new URL(AUTH_LOGIN_PATHNAME, request.url);
const loginCallbackUrl = new URL(request.url);
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()
);
return NextResponse.redirect(redirectUrl);
}
return res;
}
export const config = {
unstable_allowDynamic: [
'**/node_modules/lodash/**',
'**/node_modules/@wix/**',
],
};