Simplify the cookiePath logic

Signed-off-by: Marcus Eide <eide@spotify.com>
This commit is contained in:
Marcus Eide
2022-01-24 14:22:54 +01:00
parent 033493a8af
commit 71a2fbaa58
2 changed files with 7 additions and 11 deletions
@@ -116,6 +116,7 @@ describe('OAuthProvider Utils', () => {
expect(getCookieConfig(mockAuthUrl, 'test-provider')).toMatchObject({
cookieDomain: 'domain.org',
cookiePath: '/auth/test-provider',
secure: false,
});
});
@@ -126,6 +127,7 @@ describe('OAuthProvider Utils', () => {
expect(getCookieConfig(mockAuthUrl, 'test-provider')).toMatchObject({
cookieDomain: 'domain.org',
cookiePath: '/auth/test-provider',
secure: false,
});
});
+5 -11
View File
@@ -63,17 +63,11 @@ export const getCookieConfig = (authUrl: URL, providerId: string) => {
const secure = protocol === 'https:';
// If the provider supports callbackUrls, the pathname will
// contain the complete path to the frame handler.
// The regex captures the leading path including the provider.
//
// Example match: /api/auth/provider/handler/frame
// Example result: /api/auth/provider
const matcher = pathname.match(
new RegExp(`(?<path>^\/.*?\/${providerId})\/handler\/frame$`),
)?.groups;
// If there's no match we can manually construct the path
const cookiePath = matcher?.path ?? `${pathname}/${providerId}`;
// contain the complete path to the frame handler so we need
// to slice off the trailing part of the path.
const cookiePath = pathname.endsWith(`${providerId}/handler/frame`)
? pathname.slice(0, -'/handler/frame'.length)
: `${pathname}/${providerId}`;
return {
cookieDomain,