Skip to content

Commit a3b397c

Browse files
committed
feat: social login redirect
1 parent f4471a4 commit a3b397c

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

web/app/account/oauth/authorize/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
} from '@remixicon/react'
2020

2121
export const OAUTH_AUTHORIZE_PENDING_KEY = 'oauth_authorize_pending'
22-
export const REDIRECT_URL_KEY = 'redirect_url'
22+
export const REDIRECT_URL_KEY = 'oauth_redirect_url'
2323

2424
function buildReturnUrl(pathname: string, search: string) {
2525
try {

web/app/components/swr-initializer.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
EDUCATION_VERIFYING_LOCALSTORAGE_ITEM,
1010
EDUCATION_VERIFY_URL_SEARCHPARAMS_ACTION,
1111
} from '@/app/education-apply/constants'
12+
import { resolvePostLoginRedirect } from '../signin/utils/post-login-redirect'
1213

1314
type SwrInitializerProps = {
1415
children: ReactNode
@@ -63,7 +64,8 @@ const SwrInitializer = ({
6364
if (searchParams.has('access_token') || searchParams.has('refresh_token')) {
6465
consoleToken && localStorage.setItem('console_token', consoleToken)
6566
refreshToken && localStorage.setItem('refresh_token', refreshToken)
66-
router.replace(pathname)
67+
const redirectUrl = resolvePostLoginRedirect(searchParams)
68+
router.replace(redirectUrl || pathname)
6769
}
6870

6971
setInit(true)

web/app/signin/utils/post-login-redirect.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,28 @@ import type { ReadonlyURLSearchParams } from 'next/navigation'
33

44
export const resolvePostLoginRedirect = (searchParams: ReadonlyURLSearchParams) => {
55
const redirectUrl = searchParams.get(REDIRECT_URL_KEY)
6-
if (redirectUrl)
7-
return decodeURIComponent(redirectUrl)
6+
if (redirectUrl) {
7+
try {
8+
localStorage.removeItem(OAUTH_AUTHORIZE_PENDING_KEY)
9+
return decodeURIComponent(redirectUrl)
10+
}
11+
catch (e) {
12+
console.error('Failed to decode redirect URL:', e)
13+
return redirectUrl
14+
}
15+
}
816

917
try {
1018
const pendingStr = localStorage.getItem(OAUTH_AUTHORIZE_PENDING_KEY)
1119
if (pendingStr) {
1220
const pending = JSON.parse(pendingStr)
1321
localStorage.removeItem(OAUTH_AUTHORIZE_PENDING_KEY)
14-
return pending?.returnUrl
22+
return pending?.returnUrl || null
1523
}
1624
}
17-
catch { }
25+
catch (e) {
26+
console.error('Failed to parse pending redirect info:', e)
27+
}
28+
1829
return null
1930
}

0 commit comments

Comments
 (0)