Feat/api jwt (#1212)

This commit is contained in:
zxhlyh
2023-09-25 12:49:16 +08:00
committed by GitHub
parent c40ee7e629
commit 227f9fb77d
15 changed files with 142 additions and 339 deletions

View File

@@ -1,7 +1,9 @@
'use client'
import { SWRConfig } from 'swr'
import { useEffect, useState } from 'react'
import type { ReactNode } from 'react'
import { useRouter, useSearchParams } from 'next/navigation'
type SwrInitorProps = {
children: ReactNode
@@ -9,13 +11,32 @@ type SwrInitorProps = {
const SwrInitor = ({
children,
}: SwrInitorProps) => {
return (
<SWRConfig value={{
shouldRetryOnError: false,
}}>
{children}
</SWRConfig>
)
const router = useRouter()
const searchParams = useSearchParams()
const consoleToken = searchParams.get('console_token')
const consoleTokenFromLocalStorage = localStorage?.getItem('console_token')
const [init, setInit] = useState(false)
useEffect(() => {
if (!(consoleToken || consoleTokenFromLocalStorage))
router.replace('/signin')
if (consoleToken) {
localStorage?.setItem('console_token', consoleToken!)
router.replace('/apps', { forceOptimisticNavigation: false })
}
setInit(true)
}, [])
return init
? (
<SWRConfig value={{
shouldRetryOnError: false,
}}>
{children}
</SWRConfig>
)
: null
}
export default SwrInitor

View File

@@ -8,6 +8,10 @@ import I18n from '@/context/i18n'
const Header = () => {
const { locale, setLocaleOnClient } = useContext(I18n)
if (localStorage?.getItem('console_token'))
localStorage.removeItem('console_token')
return <div className='flex items-center justify-between p-6 w-full'>
<div className={style.logo}></div>
<Select

View File

@@ -89,7 +89,7 @@ const NormalForm = () => {
}
try {
setIsLoading(true)
await login({
const res = await login({
url: '/login',
body: {
email,
@@ -97,7 +97,8 @@ const NormalForm = () => {
remember_me: true,
},
})
router.push('/apps')
localStorage.setItem('console_token', res.data)
router.replace('/apps')
}
finally {
setIsLoading(false)

View File

@@ -179,6 +179,10 @@ const baseFetch = <T>(
}
options.headers.set('Authorization', `Bearer ${accessTokenJson[sharedToken]}`)
}
else {
const accessToken = localStorage.getItem('console_token') || ''
options.headers.set('Authorization', `Bearer ${accessToken}`)
}
if (deleteContentType) {
options.headers.delete('Content-Type')
@@ -292,7 +296,9 @@ export const upload = (options: any): Promise<any> => {
const defaultOptions = {
method: 'POST',
url: `${API_PREFIX}/files/upload`,
headers: {},
headers: {
Authorization: `Bearer ${localStorage.getItem('console_token') || ''}`,
},
data: {},
}
options = {

View File

@@ -15,8 +15,8 @@ import type {
} from '@/models/app'
import type { BackendModel, ProviderMap } from '@/app/components/header/account-setting/model-page/declarations'
export const login: Fetcher<CommonResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
return post<CommonResponse>(url, { body })
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 setup: Fetcher<CommonResponse, { body: Record<string, any> }> = ({ body }) => {