Skip to content

Commit f1b2eed

Browse files
obdevob-robot
authored andcommitted
PullRequest: 505 feat: manage login state with local storage updates for password-free login
1 parent d07be6b commit f1b2eed

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

frontend/src/model/profile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const model = {
8282
});
8383

8484
setEncryptLocalStorage('password', '');
85+
setEncryptLocalStorage('login', 'false');
8586
history.push('/login');
8687
},
8788
},

frontend/src/page/Layout/index.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import GlobalStyle from './GlobalStyle';
2929
import { getEncryptLocalStorage, setEncryptLocalStorage } from '@/util';
3030
import { getStatistics } from '@/service/obshell/ob';
3131
import { telemetryReport } from '@/service/custom';
32+
import * as obService from '@/service/obshell/ob';
3233
import moment from 'moment';
3334

3435
interface LayoutProps {
@@ -71,8 +72,14 @@ const Layout: React.FC<LayoutProps> = ({ children, location }) => {
7172
},
7273
});
7374

75+
// 用来触发非免密场景下空密码 401 跳到登录页
76+
useRequest(obService.getObInfo, {
77+
ready: !isPasswordFreeLogin,
78+
});
79+
7480
const telemetryTime = getEncryptLocalStorage('telemetryTime');
7581
const isTelemetryOutdated = !telemetryTime || moment().diff(moment(telemetryTime), 'hours') >= 1;
82+
const isLogin = getEncryptLocalStorage('login') === 'true';
7683

7784
useRequest(
7885
() =>
@@ -83,7 +90,9 @@ const Layout: React.FC<LayoutProps> = ({ children, location }) => {
8390
ready:
8491
isTelemetryOutdated &&
8592
// 如果是免密登录场景,获取到 secret 后在进行 statistics 请求,避免 401 错误
86-
(isPasswordFreeLogin ? !loading : true),
93+
(isPasswordFreeLogin ? !loading : isLogin),
94+
// 登录状态变化后,重新发起请求,避免首次失败后不再发起请求
95+
refreshDeps: [isLogin],
8796
// 一小时 + 5秒 轮训一次。5s 是为了避免请求 telemetry 接口时 ,时间差(telemetryTime 判断)导致的请求失败
8897
pollingInterval: 1000 * 60 * 60 + 5000,
8998
onSuccess: res => {

frontend/src/page/Login/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const LoginPage: React.FC<LoginPageProps> = ({
8989
});
9090

9191
setEncryptLocalStorage('password', password);
92+
setEncryptLocalStorage('login', 'true');
9293
getObInfo();
9394
};
9495

frontend/src/util/request.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import encrypt from '@/util/encrypt';
3030
import { aesEncrypt } from '@/util/aes';
3131
import { cloneDeep } from 'lodash';
3232
import queryString from 'query-string';
33-
import { getEncryptLocalStorage } from '@/util';
33+
import { getEncryptLocalStorage, setEncryptLocalStorage } from '@/util';
3434

3535
const statusCodeMessage = {
3636
400: formatMessage({
@@ -172,6 +172,8 @@ const errorHandler = ({ request, response, data }) => {
172172
const { status } = response || {};
173173
// 401 状态为未登录情况,不展示接口错误信息,直接跳转登录页,因此需要单独处理
174174
if (status === 401) {
175+
setEncryptLocalStorage('login', 'false');
176+
175177
// 未登录状态,清空 tracert 的用户标识
176178
// tracert.set({
177179
// roleId: null,

0 commit comments

Comments
 (0)