Skip to content

Commit

Permalink
Add more visitor auth cookie combinations with /v/ for backwards comp…
Browse files Browse the repository at this point in the history
…atibility (#2503)
  • Loading branch information
taranvohra authored Sep 30, 2024
1 parent 1152445 commit 4df235d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/gitbook/src/lib/visitor-auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ describe('getVisitorAuthToken', () => {
const request = nextRequest('https://example.com');
expect(getVisitorAuthToken(request, request.nextUrl)).toBeUndefined();
});

// For backwards compatibility
it('should return the token from the cookie of a /v/ path when the url does not have /v/', () => {
const request = nextRequest('https://example.com/hello/space1/cool', {
[getVisitorAuthCookieName('/')]: { value: getVisitorAuthCookieValue('/', 'no') },
[getVisitorAuthCookieName('/hello/v/space1/')]: {
value: getVisitorAuthCookieValue('/hello/v/space1/', 'gotcha'),
},
});

const visitorAuth = getVisitorAuthToken(request, request.nextUrl);
assertVisitorAuthCookieValue(visitorAuth);
expect(visitorAuth.token).toEqual('gotcha');
});
});

function assertVisitorAuthCookieValue(value: unknown): asserts value is VisitorAuthCookieValue {
Expand Down
6 changes: 6 additions & 0 deletions packages/gitbook/src/lib/visitor-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ function getUrlBasePathCombinations(url: URL | NextRequest['nextUrl']): string[]

for (let index = 0; index < parts.length; index++) {
baseNames.push('/' + parts.slice(0, index + 1).join('/') + '/');
// For backwards compatibility, we also add the base path with the `/v/` prefix if it's not already there
if (parts.length > 1 && index >= 1 && ![parts[0], parts[1]].includes('v')) {
baseNames.push(
'/' + parts.slice(0, 1) + '/v/' + parts.slice(1, index + 1).join('/') + '/',
);
}
}

return baseNames.reverse();
Expand Down

0 comments on commit 4df235d

Please sign in to comment.