Skip to content

Commit

Permalink
fix(edge): Compatibility handler for old edge cookies handler
Browse files Browse the repository at this point in the history
  • Loading branch information
igneel64 committed Jul 4, 2022
1 parent 107d70e commit 4e42a11
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions packages/edge/src/vercel-edge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,15 @@ export function withEdgeMiddlewareAuth(
): any {
return async function clerkAuth(req: NextRequest, event: NextFetchEvent) {
const { loadUser, loadSession, jwtKey, authorizedParties } = options;
const cookieToken = req.cookies.get('__session');

const cookieToken = getCookie(req.cookies, '__session');
const clientUat = getCookie(req.cookies, '__client_uat');

const headerToken = req.headers.get('authorization');
const { status, interstitial, sessionClaims, errorReason } = await vercelEdgeBase.getAuthState({
cookieToken,
headerToken,
clientUat: req.cookies.get('__client_uat'),
clientUat,
origin: req.headers.get('origin'),
host: req.headers.get('host') as string,
userAgent: req.headers.get('user-agent'),
Expand Down Expand Up @@ -135,3 +138,13 @@ export function withEdgeMiddlewareAuth(
return handler(authRequest, event);
};
}

/* As of Next.js 12.2, the req.cookies API is accessed using .get, on previous releases, it used plain object access. To support both without breaking previous versions, we can use this simple check. */
function getCookie(cookies: NextRequest['cookies'], name: string) {
if (typeof cookies.get === 'function') {
return cookies.get(name);
} else {
// @ts-expect-error
return cookies[name];
}
}

0 comments on commit 4e42a11

Please sign in to comment.