Skip to content

Commit

Permalink
wip debugging handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
BRKalow committed Nov 30, 2023
1 parent aff4caa commit 8935ded
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
4 changes: 3 additions & 1 deletion packages/nextjs/src/server/authMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AuthObject } from '@clerk/backend';
import { buildRequestUrl, constants } from '@clerk/backend';
import { buildRequestUrl, constants, verifyToken } from '@clerk/backend';
import { DEV_BROWSER_JWT_MARKER, setDevBrowserJWTInURL } from '@clerk/shared/devBrowser';
import { isDevelopmentFromSecretKey } from '@clerk/shared/keys';
import { eventMethodCalled } from '@clerk/shared/telemetry';
Expand Down Expand Up @@ -207,7 +207,9 @@ const authMiddleware: AuthMiddleware = (...args: unknown[]) => {
console.log('skt 2.1 reqWithCookie', reqWithCookie.headers.get('cookie'));
// todo: handle normal signed in or signed out
const requestState = await authenticateRequest(reqWithCookie, options);

if (requestState.status === 'handshake') {
console.log(requestState);
throw new Error('invalid state, should be signed in or signed out at this point');
}

Expand Down
12 changes: 10 additions & 2 deletions packages/nextjs/src/server/handshake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const startHandshake = async (request: Request, _opts: WithAuthOptions) =

const dbJwt = req.clerkUrl.searchParams.get('__clerk_db_jwt') || req.cookies.get('__clerk_db_jwt');
if (dbJwt) {
url.searchParams.set('__dev_session', dbJwt);
url.searchParams.set('__clerk_db_jwt', dbJwt);
}

return Response.redirect(url, 307);
Expand All @@ -82,13 +82,21 @@ const parseHandshakeResult = async (handshakeResult: string, opts: WithAuthOptio
'Access-Control-Allow-Credentials': 'true',
});
let token = '';

console.log('cookies', cookiesToSet);
console.log('headers before', headersToSet);

cookiesToSet.forEach((x: string) => {
headersToSet.append('Set-Cookie', x);
if (x.startsWith('__session=')) {
token = x.split(';')[0].substring(10);
}
});

console.log('headers', headersToSet);

console.log('token', token);

if (token === '') {
return { status: AuthenticateRequestStatus.SignedOut, headers: headersToSet };
}
Expand Down Expand Up @@ -120,7 +128,7 @@ const authenticateRequestWithCookies = async (req: ClerkRequest, opts: WithAuthO
status: AuthenticateRequestStatus.Handshake,
reason: 'new-dev-browser',
headers: new Headers({
'Set-Cookie': serializeCookie('__dev_session', newDevBrowser, { path: '/' }),
'Set-Cookie': serializeCookie('__clerk_db_jwt', newDevBrowser, { path: '/' }),
}),
};
}
Expand Down
12 changes: 8 additions & 4 deletions packages/nextjs/src/utils/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ export const mergeResponses = (...responses: (NextResponse | Response | null | u

for (const response of normalisedResponses) {
response.headers.forEach((value: string, name: string) => {
finalResponse.headers.set(name, value);
if (name.toLocaleLowerCase() === 'set-cookie') {
finalResponse.headers.append(name, value);
} else {
finalResponse.headers.set(name, value);
}
});

response.cookies.getAll().forEach(cookie => {
finalResponse.cookies.set(cookie.name, cookie.value);
});
// response.cookies.getAll().forEach(cookie => {
// finalResponse.cookies.set(cookie.name, cookie.value);
// });
}

return finalResponse;
Expand Down

0 comments on commit 8935ded

Please sign in to comment.