Skip to content

Commit

Permalink
fix: logged in redirection (#18362)
Browse files Browse the repository at this point in the history
  • Loading branch information
daibhin authored Nov 2, 2023
1 parent 9e39882 commit 90ca04b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ describe('redirectToLoggedInInstance', () => {
['handles the empty string', '', null],
['handles the sneaky string', ' ', null],
['handles not URLs', 'yo ho ho', null],
['handles EU', 'https://eu.posthog.com', 'EU'],
['handles US', 'https://app.posthog.com', 'US'],
['handles leading quotes', '"https://eu.posthog.com', 'EU'],
['handles trailing quotes', 'https://eu.posthog.com"', 'EU'],
['handles wrapping quotes', '"https://eu.posthog.com"', 'EU'],
['handles ports', 'https://app.posthog.com:8123', 'US'],
['handles longer urls', 'https://app.posthog.com:1234?query=parameter#hashParam', 'US'],
['handles EU', 'https://eu.posthog.com', 'eu'],
['handles US', 'https://app.posthog.com', 'app'],
['handles leading quotes', '"https://eu.posthog.com', 'eu'],
['handles trailing quotes', 'https://eu.posthog.com"', 'eu'],
['handles wrapping quotes', '"https://eu.posthog.com"', 'eu'],
['handles ports', 'https://app.posthog.com:8123', 'app'],
['handles longer urls', 'https://app.posthog.com:1234?query=parameter#hashParam', 'app'],
])('%s', (_name, cookie, expected) => {
expect(cleanedCookieSubdomain(cookie)).toEqual(expected)
})
Expand Down
42 changes: 28 additions & 14 deletions frontend/src/scenes/authentication/redirectToLoggedInInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ const PH_CURRENT_INSTANCE = 'ph_current_instance'

const REDIRECT_TIMEOUT = 2500

export function cleanedCookieSubdomain(loggedInInstance: string | null): 'EU' | 'US' | null {
type Subdomain = 'eu' | 'app'

export function cleanedCookieSubdomain(loggedInInstance: string | null): Subdomain | null {
try {
// replace '"' as for some reason the cookie value is wrapped in quotes e.g. "https://eu.posthog.com"
const url = loggedInInstance?.replace(/"/g, '')
Expand All @@ -41,9 +43,9 @@ export function cleanedCookieSubdomain(loggedInInstance: string | null): 'EU' |
const hostname = new URL(url).hostname
switch (hostname) {
case 'app.posthog.com':
return 'US'
return 'app'
case 'eu.posthog.com':
return 'EU'
return 'eu'
default:
return null
}
Expand All @@ -54,6 +56,15 @@ export function cleanedCookieSubdomain(loggedInInstance: string | null): 'EU' |
}
}

function regionFromSubdomain(subdomain: Subdomain): 'EU' | 'US' {
switch (subdomain) {
case 'app':
return 'US'
case 'eu':
return 'EU'
}
}

export function redirectIfLoggedInOtherInstance(): (() => void) | undefined {
const currentSubdomain = window.location.hostname.split('.')[0]

Expand All @@ -79,17 +90,20 @@ export function redirectIfLoggedInOtherInstance(): (() => void) | undefined {
window.location.assign(newUrl.href)
}

lemonToast.info(`Redirecting to your logged-in account in the Cloud ${loggedInSubdomain} region`, {
button: {
label: 'Cancel',
action: () => {
cancelClicked = true
lemonToast.info(
`Redirecting to your logged-in account in the Cloud ${regionFromSubdomain(loggedInSubdomain)} region`,
{
button: {
label: 'Cancel',
action: () => {
cancelClicked = true
},
},
},
onClose: closeToastAction,
// we want to force the user to click the cancel button as otherwise the default close will still redirect
closeButton: false,
autoClose: REDIRECT_TIMEOUT,
})
onClose: closeToastAction,
// we want to force the user to click the cancel button as otherwise the default close will still redirect
closeButton: false,
autoClose: REDIRECT_TIMEOUT,
}
)
}
}

0 comments on commit 90ca04b

Please sign in to comment.