-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathserverRedirectWithAuth.ts
28 lines (26 loc) · 1.23 KB
/
serverRedirectWithAuth.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
// Middleware runs on the server side, before clerk-js is loaded, that's why we need Cookies.
import type { ClerkRequest } from '@clerk/backend/internal';
import { constants } from '@clerk/backend/internal';
import { DEV_BROWSER_JWT_KEY, setDevBrowserJWTInURL } from '@clerk/shared/devBrowser';
import { isDevelopmentFromSecretKey } from '@clerk/shared/keys';
import { NextResponse } from 'next/server';
/**
* Grabs the dev browser JWT from cookies and appends it to the redirect URL when redirecting to cross-origin.
*/
export const serverRedirectWithAuth = (clerkRequest: ClerkRequest, res: Response, opts: { secretKey: string }) => {
const location = res.headers.get('location');
const shouldAppendDevBrowser = res.headers.get(constants.Headers.ClerkRedirectTo) === 'true';
if (
shouldAppendDevBrowser &&
!!location &&
isDevelopmentFromSecretKey(opts.secretKey) &&
clerkRequest.clerkUrl.isCrossOrigin(location)
) {
const dbJwt = clerkRequest.cookies.get(DEV_BROWSER_JWT_KEY) || '';
// Next.js 12.1+ allows redirects only to absolute URLs
const url = new URL(location);
const urlWithDevBrowser = setDevBrowserJWTInURL(url, dbJwt);
return NextResponse.redirect(urlWithDevBrowser.href, res);
}
return res;
};