fix: get access_token and refresh_token from the url, save to localStorage

This commit is contained in:
NFish 2024-10-11 16:37:00 +08:00
parent d498f4e55e
commit 983b64ffe6
4 changed files with 14 additions and 9 deletions

View File

@ -13,16 +13,18 @@ const SwrInitor = ({
}: SwrInitorProps) => {
const router = useRouter()
const searchParams = useSearchParams()
const consoleToken = searchParams.get('console_token')
const consoleToken = searchParams.get('access_token')
const refreshToken = searchParams.get('refresh_token')
const consoleTokenFromLocalStorage = localStorage?.getItem('console_token')
const [init, setInit] = useState(false)
useEffect(() => {
if (!(consoleToken || consoleTokenFromLocalStorage))
if (!(consoleToken || refreshToken || consoleTokenFromLocalStorage))
router.replace('/signin')
if (consoleToken) {
if (consoleToken && refreshToken) {
localStorage?.setItem('console_token', consoleToken!)
localStorage?.setItem('refresh_token', refreshToken!)
router.replace('/apps', { forceOptimisticNavigation: false } as any)
}
setInit(true)

View File

@ -95,7 +95,8 @@ const NormalForm = () => {
},
})
if (res.result === 'success') {
localStorage.setItem('console_token', res.data)
localStorage.setItem('console_token', res.data.access_token)
localStorage.setItem('refresh_token', res.data.refresh_token)
router.replace('/apps')
}
else {

View File

@ -16,7 +16,8 @@ const UserSSOForm: FC<UserSSOFormProps> = ({
protocol,
}) => {
const searchParams = useSearchParams()
const consoleToken = searchParams.get('console_token')
const consoleToken = searchParams.get('access_token')
const refreshToken = searchParams.get('refresh_token')
const message = searchParams.get('message')
const router = useRouter()
@ -25,7 +26,8 @@ const UserSSOForm: FC<UserSSOFormProps> = ({
const [isLoading, setIsLoading] = useState(false)
useEffect(() => {
if (consoleToken) {
if (refreshToken && consoleToken) {
localStorage.setItem('refresh_token', refreshToken)
localStorage.setItem('console_token', consoleToken)
router.replace('/apps')
}
@ -36,7 +38,7 @@ const UserSSOForm: FC<UserSSOFormProps> = ({
message,
})
}
}, [])
}, [consoleToken, refreshToken, message, router])
const handleSSOLogin = () => {
setIsLoading(true)

View File

@ -38,8 +38,8 @@ import type {
import type { RETRIEVE_METHOD } from '@/types/app'
import type { SystemFeatures } from '@/types/feature'
export const login: Fetcher<CommonResponse & { data: string }, { url: string; body: Record<string, any> }> = ({ url, body }) => {
return post(url, { body }) as Promise<CommonResponse & { data: string }>
export const login: Fetcher<CommonResponse & { data: { access_token: string;refresh_token: string } }, { url: string; body: Record<string, any> }> = ({ url, body }) => {
return post(url, { body }) as Promise<CommonResponse & { data: { access_token: string;refresh_token: string } }>
}
export const setup: Fetcher<CommonResponse, { body: Record<string, any> }> = ({ body }) => {