From 2a9aa41946cad86c9db07c0ef0aeec4d9c3a7ece Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Wed, 18 May 2022 15:50:03 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20luna=20=E6=B7=BB=E5=8A=A0=20session=20g?= =?UTF-8?q?uard=20(#599)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * perf: luna 添加 session guard * perf: 都是用 gotoLogin * perf: 修改避免繁琐 Co-authored-by: ibuler --- src/app/services/app.ts | 21 +++++++++++++++++++-- src/app/utils/common.ts | 7 +++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/app/services/app.ts b/src/app/services/app.ts index 535076ad..3119efb2 100644 --- a/src/app/services/app.ts +++ b/src/app/services/app.ts @@ -8,9 +8,15 @@ import {LocalStorageService, LogService} from './share'; import {SettingService} from '@app/services/setting'; import {AuthInfo, ConnectData, SystemUser, TreeNode, Endpoint, Protocol, View} from '@app/model'; import * as CryptoJS from 'crypto-js'; +import {getCookie, setCookie} from '@app/utils/common'; declare function unescape(s: string): string; +function gotoLogin() { + const currentPath = encodeURI(document.location.pathname + document.location.search); + window.location.href = document.location.origin + '/core/auth/login/?next=' + currentPath; +} + @Injectable() export class AppService { // user:User = user ; @@ -56,6 +62,18 @@ export class AppService { } return; } + + // Determine whether the user has logged in + const sessionExpire = getCookie('jms_session_expire'); + if (!sessionExpire) { + gotoLogin(); + return; + } else if (sessionExpire === 'close') { + setInterval(() => { + setCookie('jms_session_expire', sessionExpire, 120); + }, 10 * 1000); + } + this._http.getUserProfile().subscribe( user => { Object.assign(User, user); @@ -65,10 +83,9 @@ export class AppService { err => { // this._logger.error(err); User.logined = false; - const currentPath = encodeURI(document.location.pathname + document.location.search); const token = this.getQueryString('token'); if (!token) { - window.location.href = document.location.origin + '/core/auth/login/?next=' + currentPath; + gotoLogin(); } // this._router.navigate(['login']); }, diff --git a/src/app/utils/common.ts b/src/app/utils/common.ts index cffde261..82328383 100644 --- a/src/app/utils/common.ts +++ b/src/app/utils/common.ts @@ -33,6 +33,13 @@ export function newTerminal(fontSize?: number) { }); } +export function setCookie(name, value, seconds) { + const d = new Date(); + d.setTime(d.getTime() + seconds * 1000); + const expires = 'expires=' + d.toUTCString(); + document.cookie = name + '=' + value + '; ' + expires; +} + export function getCookie(name: string): string { let cookieValue = null; if (document.cookie && document.cookie !== '') {