Skip to content

Commit 32a14fb

Browse files
committed
add redirect persistence for SSO
1 parent 4186fa6 commit 32a14fb

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

src/components/auth/form/login.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { useSession } from "@/hooks/use-session"
2020
import { cn } from "@/lib/utils"
2121
import { toast } from "@/lib/toast"
2222
import { truncator } from "@/lib/truncate"
23+
import { setPendingRedirect } from "@/lib/auth"
2324
import { getRecentAccounts } from "@/lib/accounts"
2425

2526
import * as Auth from "@/components/auth"
@@ -591,11 +592,13 @@ function SsoStep({
591592
onCancel: () => void
592593
}) {
593594
const [loading, setLoading] = useState(false)
595+
const { redirect } = useSearch({ from: "/$accountId/auth/login" })
594596

595597
function onContinue() {
596598
if (!redirectUrl) return
597599

598600
setLoading(true)
601+
if (redirect) setPendingRedirect(redirect)
599602
window.location.href = redirectUrl
600603
}
601604

src/lib/auth.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,27 @@ interface ResetToken {
1919
token: string
2020
}
2121

22+
const REDIRECT_STORAGE_KEY = "keygen.auth.redirect"
23+
2224
export function parseRedirect(value: unknown): string | undefined {
2325
return typeof value === "string" && /^\/goto(?:[/?#]|$)/.test(value)
2426
? value
2527
: undefined
2628
}
2729

30+
export function setPendingRedirect(value: string): void {
31+
window.sessionStorage.setItem(REDIRECT_STORAGE_KEY, value)
32+
}
33+
34+
export function takePendingRedirect(): string | undefined {
35+
const value = parseRedirect(
36+
window.sessionStorage.getItem(REDIRECT_STORAGE_KEY),
37+
)
38+
window.sessionStorage.removeItem(REDIRECT_STORAGE_KEY)
39+
40+
return value
41+
}
42+
2843
// parse a reset token from the URL query string
2944
export function parseResetToken(
3045
value: string | undefined | null,

src/routes/$accountId/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
import { createFileRoute, redirect } from "@tanstack/react-router"
22

33
import { restoreSession } from "@/keygen/session"
4+
import { takePendingRedirect } from "@/lib/auth"
45

56
export const Route = createFileRoute("/$accountId/")({
67
loader: async ({ params }) => {
7-
// SSO returns the browser to the slug-addressed account root. Resolve the
8+
// SSO returns the browser to the slug-addressed account root; resolve the
89
// session so we can land on the account's stable id rather than the slug.
910
const { accountId } = await restoreSession()
1011

11-
return redirect({
12+
const pendingRedirect = takePendingRedirect()
13+
if (pendingRedirect) {
14+
redirect({ href: pendingRedirect, replace: true, throw: true })
15+
return
16+
}
17+
18+
redirect({
1219
to: "/$accountId/app",
1320
params: { accountId: accountId ?? params.accountId },
1421
replace: true,
22+
throw: true,
1523
})
1624
},
1725
component: () => null,

0 commit comments

Comments
 (0)