Skip to content
This repository was archived by the owner on Jan 29, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion packages/react-contexts/src/session-context/browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
SessionAvailabilityContext,
SessionControllers,
} from './context'
import { getRedirectUrl } from './redirect'

export interface InBrowserSessionContextProviderProps {
initialSessionAvailability: boolean
Expand Down Expand Up @@ -49,9 +50,36 @@ export function InBrowserSessionContextProvider({
}, [])

const logout = useCallback<SessionControllers['logout']>(async () => {
await authGuardedFetchers.put('/api/users/logout')
const response = await authGuardedFetchers.put<{ redirectUrl: string }>(
'/api/users/logout',
)

clearUserState()

if (response === 'NEED_LOGIN') {
return
}

const isNolConnectedUser =
response.ok &&
response.status === 200 &&
!!response.parsedBody.redirectUrl

if (isNolConnectedUser) {
const redirectLocation = response.parsedBody.redirectUrl

if (!redirectLocation) {
captureHttpError(response)
window.location.reload()
return
}

const redirectUrl = getRedirectUrl(redirectLocation)

window.location.href = redirectUrl
return
}

window.location.reload()
}, [clearUserState])

Expand Down
27 changes: 27 additions & 0 deletions packages/react-contexts/src/session-context/redirect.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getRedirectUrl } from './redirect'

describe('getRedirectUrl', () => {
const mockUrl = 'https://triple.guide'

Object.defineProperty(window, 'location', {
value: new URL(mockUrl),
})

it('should return redirect url with redirect query string', () => {
const result = getRedirectUrl('https://example.com')
expect(result).toBe(
`https://example.com?redirectUrl=${encodeURIComponent(
window.location.href,
)}`,
)
})

it('should return redirect url with redirect query string and original href query string', () => {
const result = getRedirectUrl('https://example.com?code=123')
expect(result).toBe(
`https://example.com?code=123&redirectUrl=${encodeURIComponent(
window.location.href,
)}`,
)
})
})
17 changes: 17 additions & 0 deletions packages/react-contexts/src/session-context/redirect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { generateUrl } from '@titicaca/view-utilities'
import qs from 'qs'

export function getRedirectUrl(href: string) {
const currentUrl = decodeURI(window.location.href)

const redirectUrl = generateUrl(
{
query: qs.stringify({
redirectUrl: currentUrl,
}),
},
href,
)

return redirectUrl
}
Loading