feat: 项目接口联调
This commit is contained in:
73
src/features/authGuide.tsx
Normal file
73
src/features/authGuide.tsx
Normal file
@@ -0,0 +1,73 @@
|
||||
import {useQuery, useQueryClient} from '@tanstack/react-query'
|
||||
import {type PropsWithChildren, useEffect} from 'react'
|
||||
|
||||
import {login, validateToken} from '@/api/auth.ts'
|
||||
import {userAssets} from '@/api/user.ts'
|
||||
import {queryKeys} from '@/lib/queryKeys.ts'
|
||||
import {useUserStore} from '@/store/user.ts'
|
||||
|
||||
export function AuthGuide({children}: PropsWithChildren) {
|
||||
const queryClient = useQueryClient()
|
||||
const setUserInfo = useUserStore((state) => state.setUserInfo)
|
||||
const setAuthInfo = useUserStore((state) => state.setAuthInfo)
|
||||
const setAssetsInfo = useUserStore((state) => state.setAssetsInfo)
|
||||
const clearUserInfo = useUserStore((state) => state.clearUserInfo)
|
||||
const authBootstrapQuery = useQuery({
|
||||
queryKey: queryKeys.authBootstrap,
|
||||
queryFn: async () => {
|
||||
const loginResponse = await login({username: '+60777777777'})
|
||||
const userInfo = loginResponse.data.userInfo
|
||||
const validateResponse = await validateToken(userInfo.token)
|
||||
const authInfo = validateResponse.data
|
||||
const assetsResponse = await userAssets({
|
||||
session_id: authInfo.session_id,
|
||||
})
|
||||
|
||||
return {
|
||||
userInfo,
|
||||
authInfo,
|
||||
assetsInfo: assetsResponse.data,
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (!authBootstrapQuery.data) {
|
||||
return
|
||||
}
|
||||
|
||||
setUserInfo(authBootstrapQuery.data.userInfo)
|
||||
setAuthInfo(authBootstrapQuery.data.authInfo)
|
||||
setAssetsInfo(authBootstrapQuery.data.assetsInfo)
|
||||
queryClient.setQueryData(queryKeys.assets(authBootstrapQuery.data.authInfo.session_id), authBootstrapQuery.data.assetsInfo)
|
||||
}, [authBootstrapQuery.data, queryClient, setAssetsInfo, setAuthInfo, setUserInfo])
|
||||
|
||||
useEffect(() => {
|
||||
if (!authBootstrapQuery.isError) {
|
||||
return
|
||||
}
|
||||
|
||||
clearUserInfo()
|
||||
}, [authBootstrapQuery.isError, clearUserInfo])
|
||||
|
||||
if (authBootstrapQuery.isPending) {
|
||||
return (
|
||||
<div className="flex min-h-screen items-center justify-center bg-[#08070E] px-6 text-center text-[14px] text-white/68">
|
||||
Loading account data...
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (authBootstrapQuery.isError) {
|
||||
return (
|
||||
<div className="flex min-h-screen items-center justify-center bg-[#08070E] px-6 text-center">
|
||||
<div className="max-w-[420px] rounded-[14px] border border-white/10 bg-white/4 px-[18px] py-[16px]">
|
||||
<div className="text-[16px] font-semibold text-white">Authentication failed</div>
|
||||
<div className="mt-[8px] text-[13px] leading-[1.6] text-white/58">Please refresh and try again.</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return <>{children}</>
|
||||
}
|
||||
Reference in New Issue
Block a user