From 557384e614d3b0d49c1dae1073a169b0b98488d5 Mon Sep 17 00:00:00 2001 From: Zzm0809 <934230207@qq.com> Date: Thu, 7 Mar 2024 23:26:39 +0800 Subject: [PATCH] [Fix] Fix token may get empty and cause page crash (#3254) Co-authored-by: Zzm0809 --- .../resources/i18n/messages_en_US.properties | 2 +- .../resources/i18n/messages_zh_CN.properties | 2 +- dinky-web/src/pages/Other/Login/function.tsx | 2 +- dinky-web/src/pages/Other/Login/index.tsx | 17 ++++++++++++----- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/dinky-common/src/main/resources/i18n/messages_en_US.properties b/dinky-common/src/main/resources/i18n/messages_en_US.properties index 3552212a24..6ef61c096f 100644 --- a/dinky-common/src/main/resources/i18n/messages_en_US.properties +++ b/dinky-common/src/main/resources/i18n/messages_en_US.properties @@ -35,7 +35,7 @@ test.msg.job.log.url=Click to view the exception log for this task user.assign.role.success=User Assign Role Success global.params.check.error.value=Field: {0}, Illegal Value: {1} change.password.success=Change Password Success -user.not.exist=User:{0} Not Exist +user.not.exist=User Not Exist refresh.success=Refresh Successfully ds.get.node.list.error=Get Node List Error ldap.default.tenant.nofound=The LDAP default tenant does not exist diff --git a/dinky-common/src/main/resources/i18n/messages_zh_CN.properties b/dinky-common/src/main/resources/i18n/messages_zh_CN.properties index c6f8a7cb68..5519d0d46a 100644 --- a/dinky-common/src/main/resources/i18n/messages_zh_CN.properties +++ b/dinky-common/src/main/resources/i18n/messages_zh_CN.properties @@ -35,7 +35,7 @@ test.msg.job.log.url=点击查看该任务的异常日志 user.assign.role.success=用户分配角色成功 global.params.check.error.value=字段: {0}, 不合法的值: {1} change.password.success=修改密码成功 -user.not.exist=用户:{0} 不存在 +user.not.exist=用户不存在 refresh.success=刷新成功 ds.get.node.list.error=节点获取失败 ldap.default.tenant.nofound=LDAP默认租户不存在 diff --git a/dinky-web/src/pages/Other/Login/function.tsx b/dinky-web/src/pages/Other/Login/function.tsx index a14180b890..85f7a1c572 100644 --- a/dinky-web/src/pages/Other/Login/function.tsx +++ b/dinky-web/src/pages/Other/Login/function.tsx @@ -36,7 +36,7 @@ export const gotoRedirectUrl = () => { export const redirectToLogin = () => { //TODO: 弹出确认框 - window.location.href = '/login'; + window.location.href = '/user/login'; }; export const initSomeThing = () => { diff --git a/dinky-web/src/pages/Other/Login/index.tsx b/dinky-web/src/pages/Other/Login/index.tsx index 15120773cc..1c32c35762 100644 --- a/dinky-web/src/pages/Other/Login/index.tsx +++ b/dinky-web/src/pages/Other/Login/index.tsx @@ -24,7 +24,7 @@ import LangSwitch from '@/pages/Other/Login/LangSwitch'; import { chooseTenantSubmit, login, queryDataByParams } from '@/services/BusinessCrud'; import { API } from '@/services/data'; import { API_CONSTANTS } from '@/services/endpoints'; -import { UserBaseInfo } from '@/types/AuthCenter/data'; +import { SaTokenInfo, UserBaseInfo } from '@/types/AuthCenter/data.d'; import { setTenantStorageAndCookie } from '@/utils/function'; import { useLocalStorage } from '@/utils/hook/useLocalStorage'; import { l } from '@/utils/intl'; @@ -67,7 +67,8 @@ const Login: React.FC = () => { * When the token is expired, redirect to login */ useEffect(() => { - const expirationTime = JSON.parse(JSON.stringify(localStorageOfToken)).tokenTimeout ?? 0; // GET TOKEN TIMEOUT + const expirationTime = + (JSON.parse(JSON.stringify(localStorageOfToken)) as SaTokenInfo)?.tokenTimeout ?? 0; // GET TOKEN TIMEOUT let timeRemaining = 0; let timer: NodeJS.Timeout; if (expirationTime > 0) { @@ -138,9 +139,15 @@ const Login: React.FC = () => { const result = await login({ ...values }); if (result.code === 0) { // if login success then get token info and set it to local storage - await queryDataByParams(API_CONSTANTS.TOKEN_INFO).then((res) => - setLocalStorageOfToken(JSON.stringify(res)) - ); + await queryDataByParams(API_CONSTANTS.TOKEN_INFO).then((res) => { + console.log(res); + if (res) { + setLocalStorageOfToken(JSON.stringify(res)); + } else { + // 如果没有获取到token信息,直接跳转到登录页 + redirectToLogin(); + } + }); } setInitialState((s) => ({ ...s, currentUser: result.data })); await SuccessMessageAsync(l('login.result', '', { msg: result.msg, time: result.time }));