Skip to content

Commit a19e5f9

Browse files
fix redirect login authectication
1 parent 9a593dc commit a19e5f9

2 files changed

Lines changed: 228 additions & 64 deletions

File tree

packages/guard-shim-react18/src/index.tsx

Lines changed: 126 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ export class Guard {
123123
return options
124124
}
125125

126+
private async getRequestHost() {
127+
if (this.options.host) {
128+
return this.options.host
129+
}
130+
131+
const publicConfig = await this.then()
132+
if (publicConfig.requestHostname) {
133+
return `https://${publicConfig.requestHostname}`
134+
}
135+
136+
return 'https://core.authing.cn'
137+
}
138+
126139
private async getPublicConfig(): Promise<{
127140
[prop: string]: any
128141
}> {
@@ -152,11 +165,13 @@ export class Guard {
152165
throw new Error(JSON.stringify(e))
153166
}
154167

168+
const requestHostname = await this.getRequestHost()
169+
155170
const _authClientOptions = Object.assign(
156171
{},
157172
{
158173
appId: this.options.appId,
159-
appHost: this.options.host || `https://${publicConfig.requestHostname}`,
174+
appHost: requestHostname,
160175
tenantId: this.options.tenantId,
161176
redirectUri:
162177
this.options.redirectUri || publicConfig.oidcConfig.redirect_uris[0],
@@ -241,23 +256,52 @@ export class Guard {
241256
}
242257

243258
async checkLoginStatus(): Promise<JwtTokenStatus | undefined> {
259+
// 与嵌入式登录保持一致,使用 JS SDK 缓存 token 及用户信息
244260
const authClient = await this.getAuthClient()
245-
const user = await authClient.getCurrentUser()
246261

247-
if (!user) {
262+
const userInfo = await this.trackSession()
263+
264+
if (!userInfo) {
248265
return
249266
}
250267

251-
const token = user.token
268+
authClient.tokenProvider.setUser(userInfo)
269+
270+
// 兼容老版本
271+
const accessToken = localStorage.getItem('accessToken')
252272

253-
if (!token) {
273+
if (!accessToken) {
254274
return
255275
}
256276

257-
const loginStatus: JwtTokenStatus = await authClient.checkLoginStatus(token)
277+
const requestHostname = await this.getRequestHost()
278+
279+
const options: RequestInit = {
280+
method: 'POST',
281+
credentials: 'include',
282+
headers: {
283+
'Content-Type': 'application/json'
284+
},
285+
body: JSON.stringify({
286+
token: accessToken
287+
})
288+
}
289+
290+
try {
291+
const fetchRes = await fetch(
292+
`${requestHostname}/api/v2/users/login/check-status`,
293+
options
294+
)
295+
296+
const loginStatusText = await fetchRes.text()
258297

259-
if (loginStatus.status) {
260-
return loginStatus
298+
const loginStatus: JwtTokenStatus = JSON.parse(loginStatusText)
299+
300+
if (loginStatus.code === 200 && loginStatus.status === true) {
301+
return loginStatus
302+
}
303+
} catch (e) {
304+
return
261305
}
262306
}
263307

@@ -341,11 +385,7 @@ export class Guard {
341385
codeChallenge
342386
)
343387

344-
const authClient = await this.getAuthClient()
345-
346-
const userInfo = await authClient.getUserInfoByAccessToken(access_token)
347-
348-
this.setStorageCache(access_token, id_token, userInfo)
388+
this.setTokenCache(access_token, id_token)
349389
}
350390

351391
private async getAccessTokenByCode(code: string, codeChallenge: string) {
@@ -367,14 +407,21 @@ export class Guard {
367407
}
368408
}
369409

370-
private setStorageCache(
371-
accessToken: string,
372-
idToken: string,
373-
userInfo: string
374-
) {
410+
private setTokenCache(accessToken: string, idToken: string) {
375411
localStorage.setItem('accessToken', accessToken)
376412
localStorage.setItem('idToken', idToken)
377-
localStorage.setItem('userInfo', JSON.stringify(userInfo))
413+
}
414+
415+
private clearTokenCache() {
416+
localStorage.removeItem('accessToken')
417+
localStorage.removeItem('idToken')
418+
}
419+
420+
private async clearLoginCache() {
421+
const authClient = await this.getAuthClient()
422+
localStorage.removeItem('codeChallenge')
423+
authClient.tokenProvider.clearUser()
424+
this.clearTokenCache()
378425
}
379426

380427
private parseUrlQuery() {
@@ -406,7 +453,45 @@ export class Guard {
406453
async trackSession(): Promise<User | null> {
407454
const authClient = await this.getAuthClient()
408455

409-
return authClient.getCurrentUser()
456+
const idToken =
457+
authClient.tokenProvider.getToken() || localStorage.getItem('idToken')
458+
459+
if (!idToken) {
460+
return null
461+
}
462+
463+
const publicConfig = await this.then()
464+
465+
const requestHostname = await this.getRequestHost()
466+
467+
const options: RequestInit = {
468+
method: 'GET',
469+
credentials: 'include',
470+
headers: {
471+
'Content-Type': 'application/json',
472+
'x-authing-userpool-id': publicConfig.userPoolId,
473+
Authorization: idToken
474+
}
475+
}
476+
477+
try {
478+
const fetchRes = await fetch(
479+
`${requestHostname}/api/v2/users/me`,
480+
options
481+
)
482+
483+
const userInfoText = await fetchRes.text()
484+
485+
const { code, data } = JSON.parse(userInfoText)
486+
487+
if (code === 200) {
488+
return data
489+
}
490+
491+
return null
492+
} catch (e) {
493+
return null
494+
}
410495
}
411496

412497
async logout(params: LogoutParams = {}) {
@@ -424,21 +509,30 @@ export class Guard {
424509
logoutRedirectUri = origin
425510
}
426511

427-
let logoutUri = ''
428-
const idToken = localStorage.getItem('idToken')
429512
const authClient = await this.getAuthClient()
430513

431-
await (quitCurrentDevice ? authClient.logoutCurrent() : authClient.logout())
432-
433-
if (idToken) {
434-
logoutUri = authClient.buildLogoutUrl({
435-
expert: true,
436-
redirectUri: logoutRedirectUri,
437-
idToken
438-
})
514+
try {
515+
if (quitCurrentDevice) {
516+
await authClient.logoutCurrent()
517+
} else {
518+
await authClient.logout()
519+
}
520+
} catch (error) {
521+
// 兜底 redirect 场景下,Safari 和 Firefox 开启『阻止跨站跟踪』后无法退出
522+
// 此方法只能退出当前设备
523+
const idToken =
524+
authClient.tokenProvider.getToken() || localStorage.getItem('idToken')
525+
if (idToken) {
526+
logoutRedirectUri = authClient.buildLogoutUrl({
527+
expert: true,
528+
redirectUri: logoutRedirectUri,
529+
idToken
530+
})
531+
}
532+
} finally {
533+
await this.clearLoginCache()
534+
window.location.href = logoutRedirectUri
439535
}
440-
441-
window.location.href = logoutUri || logoutRedirectUri
442536
}
443537

444538
async render() {

0 commit comments

Comments
 (0)