fix: check if setup is finished before validating access_token

This commit is contained in:
NFish 2024-10-18 15:38:38 +08:00
parent eb83336bc9
commit e1deeac706
2 changed files with 54 additions and 18 deletions

View File

@ -1,10 +1,11 @@
'use client' 'use client'
import { SWRConfig } from 'swr' import { SWRConfig } from 'swr'
import { useEffect, useState } from 'react' import { useCallback, useEffect, useState } from 'react'
import type { ReactNode } from 'react' import type { ReactNode } from 'react'
import { useRouter, useSearchParams } from 'next/navigation' import { useRouter, useSearchParams } from 'next/navigation'
import useRefreshToken from '@/hooks/use-refresh-token' import useRefreshToken from '@/hooks/use-refresh-token'
import { fetchSetupStatus } from '@/service/common'
type SwrInitorProps = { type SwrInitorProps = {
children: ReactNode children: ReactNode
@ -21,27 +22,61 @@ const SwrInitor = ({
const refreshTokenFromLocalStorage = localStorage?.getItem('refresh_token') const refreshTokenFromLocalStorage = localStorage?.getItem('refresh_token')
const [init, setInit] = useState(false) const [init, setInit] = useState(false)
useEffect(() => { const isSetupFinished = useCallback(async () => {
if (!(consoleToken || refreshToken || consoleTokenFromLocalStorage || refreshTokenFromLocalStorage)) { try {
router.replace('/signin') if (localStorage.getItem('setup_status') === 'finished')
return return true
const setUpStatus = await fetchSetupStatus()
if (setUpStatus.step !== 'finished') {
localStorage.removeItem('setup_status')
return false
}
localStorage.setItem('setup_status', 'finished')
return true
} }
if (consoleTokenFromLocalStorage && refreshTokenFromLocalStorage) catch (error) {
getNewAccessToken() console.error(error)
return false
if (consoleToken && refreshToken) {
localStorage.setItem('console_token', consoleToken)
localStorage.setItem('refresh_token', refreshToken)
getNewAccessToken().then(() => {
router.replace('/apps', { forceOptimisticNavigation: false } as any)
}).catch(() => {
router.replace('/signin')
})
} }
setInit(true)
}, []) }, [])
const setRefreshToken = useCallback(async () => {
try {
if (!(consoleToken || refreshToken || consoleTokenFromLocalStorage || refreshTokenFromLocalStorage))
return Promise.reject(new Error('No token found'))
if (consoleTokenFromLocalStorage && refreshTokenFromLocalStorage)
await getNewAccessToken()
if (consoleToken && refreshToken) {
localStorage.setItem('console_token', consoleToken)
localStorage.setItem('refresh_token', refreshToken)
await getNewAccessToken()
}
}
catch (error) {
return Promise.reject(error)
}
}, [consoleToken, refreshToken, consoleTokenFromLocalStorage, refreshTokenFromLocalStorage, getNewAccessToken])
useEffect(() => {
(async () => {
try {
const isFinished = await isSetupFinished()
if (!isFinished) {
router.replace('/install')
return
}
await setRefreshToken()
router.replace('/apps', { forceOptimisticNavigation: false } as any)
setInit(true)
}
catch (error) {
router.replace('/signin')
}
})()
}, [isSetupFinished, setRefreshToken, router])
return init return init
? ( ? (
<SWRConfig value={{ <SWRConfig value={{

View File

@ -65,6 +65,7 @@ const InstallForm = () => {
useEffect(() => { useEffect(() => {
fetchSetupStatus().then((res: SetupStatusResponse) => { fetchSetupStatus().then((res: SetupStatusResponse) => {
if (res.step === 'finished') { if (res.step === 'finished') {
localStorage.setItem('setup_status', 'finished')
window.location.href = '/signin' window.location.href = '/signin'
} }
else { else {