Skip to content

Commit

Permalink
[Fix] Fix token may get empty and cause page crash (#3254)
Browse files Browse the repository at this point in the history
Co-authored-by: Zzm0809 <[email protected]>
  • Loading branch information
Zzm0809 and Zzm0809 authored Mar 7, 2024
1 parent ca5d746 commit 557384e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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默认租户不存在
Expand Down
2 changes: 1 addition & 1 deletion dinky-web/src/pages/Other/Login/function.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const gotoRedirectUrl = () => {

export const redirectToLogin = () => {
//TODO: 弹出确认框
window.location.href = '/login';
window.location.href = '/user/login';
};

export const initSomeThing = () => {
Expand Down
17 changes: 12 additions & 5 deletions dinky-web/src/pages/Other/Login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<SaTokenInfo>(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 }));
Expand Down

0 comments on commit 557384e

Please sign in to comment.