Skip to content

Commit 12e7d44

Browse files
committed
fix: attempt to fix losing auth state
1 parent 58622e3 commit 12e7d44

File tree

2 files changed

+14
-28
lines changed

2 files changed

+14
-28
lines changed

src/App.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
import { getDefaultStore } from 'jotai/vanilla'
21
import { BrowserRouter } from 'react-router-dom'
32
import { SWRConfig } from 'swr'
43

5-
import { authAtom } from 'store/auth'
6-
import { TokenManager } from 'utils/token-manager'
7-
84
import { GlobalErrorBoundary } from './components/GlobalErrorBoundary'
95
import { I18NProvider } from './i18n/I18NProvider'
106
import { FCC } from './types'
117

12-
// jotai 在没有 Provider 时会使用默认的 store
13-
TokenManager.setAuthGetter(() => getDefaultStore().get(authAtom))
14-
TokenManager.setAuthSetter((v) => getDefaultStore().set(authAtom, v))
15-
168
export const App: FCC = ({ children }) => {
179
return (
1810
<SWRConfig

src/utils/token-manager.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
11
import { refreshAccessToken } from 'apis/auth'
2+
import { getDefaultStore } from 'jotai'
23
import { noop } from 'lodash-es'
34

4-
import { AuthState, fromCredentials } from 'store/auth'
5+
import { authAtom, fromCredentials } from 'store/auth'
56
import {
67
InvalidTokenError,
78
NetworkError,
89
TokenExpiredError,
910
UnauthorizedError,
1011
} from 'utils/error'
1112

12-
export namespace TokenManager {
13-
let getAuth: () => AuthState = () => ({})
14-
let setAuth: (set: AuthState | ((auth: AuthState) => AuthState)) => void =
15-
noop
13+
let store = getDefaultStore()
14+
let pendingGetToken: Promise<string> | undefined
1615

17-
export function setAuthGetter(get: typeof getAuth) {
18-
getAuth = get
19-
}
20-
export function setAuthSetter(set: typeof setAuth) {
21-
setAuth = set
22-
}
23-
24-
let pendingGetToken: Promise<string> | undefined
25-
26-
export async function updateAndGetToken() {
16+
export const TokenManager = {
17+
setStore(newStore: typeof store) {
18+
store = newStore
19+
},
20+
updateAndGetToken() {
2721
if (pendingGetToken) {
2822
return pendingGetToken
2923
}
3024

3125
const { token, validBefore, refreshToken, refreshTokenValidBefore } =
32-
getAuth()
26+
store.get(authAtom)
3327

3428
const endTime = +new Date(validBefore || 0) || 0
3529
const refreshEndTime = +new Date(refreshTokenValidBefore || 0) || 0
@@ -46,15 +40,15 @@ export namespace TokenManager {
4640
}
4741

4842
if (!refreshToken) {
49-
setAuth({})
43+
store.set(authAtom, {})
5044
throw new InvalidTokenError()
5145
}
5246

5347
if (refreshEndTime > now) {
5448
try {
5549
const res = await refreshAccessToken({ refreshToken })
5650

57-
setAuth(fromCredentials(res))
51+
store.set(authAtom, fromCredentials(res))
5852

5953
return res.token
6054
} catch (e) {
@@ -64,7 +58,7 @@ export namespace TokenManager {
6458
throw new TokenExpiredError()
6559
}
6660
} else {
67-
setAuth({})
61+
store.set(authAtom, {})
6862
throw new InvalidTokenError()
6963
}
7064
})()
@@ -78,5 +72,5 @@ export namespace TokenManager {
7872
.catch(noop)
7973

8074
return pendingGetToken
81-
}
75+
},
8276
}

0 commit comments

Comments
 (0)