@@ -9,10 +9,63 @@ import { MsgError } from '@/utils/message';
99const axiosCanceler = new AxiosCanceler ( ) ;
1010
1111let isRedirecting = false ;
12+ let loginStatusLoading : Promise < boolean > | null = null ;
1213let xpackEELoading : Promise < boolean > | null = null ;
1314let licenseStatusLoading : Promise < boolean > | null = null ;
15+ let loginStatusLoaded = false ;
16+ let xpackEEStatusLoaded = false ;
17+ let licenseStatusLoaded = false ;
18+ const xpackEELicenseCheckWhiteList = [ 'XpackEELicenseRequired' , 'entrance' , 'login' , 'Expired' ] ;
19+
20+ const clearLoginStatus = ( ) => {
21+ const globalStore = GlobalStore ( ) ;
22+ globalStore . setLogStatus ( false ) ;
23+ globalStore . clearAuthInfo ( ) ;
24+ globalStore . isXpackEELicensed = false ;
25+ loginStatusLoaded = false ;
26+ licenseStatusLoaded = false ;
27+ } ;
28+
29+ const loadLoginStatus = async ( ) => {
30+ const globalStore = GlobalStore ( ) ;
31+ if ( ! globalStore . isLogin ) {
32+ return false ;
33+ }
34+ if ( loginStatusLoaded ) {
35+ return true ;
36+ }
37+ if ( ! loginStatusLoading ) {
38+ loginStatusLoading = fetch ( '/api/v2/core/auth/current' , {
39+ credentials : 'include' ,
40+ headers : {
41+ CurrentNode : encodeURIComponent ( globalStore . currentNode ) ,
42+ } ,
43+ } )
44+ . then ( ( res ) => res . json ( ) )
45+ . then ( ( res ) => {
46+ const loggedIn = res ?. code === 200 ;
47+ if ( ! loggedIn ) {
48+ clearLoginStatus ( ) ;
49+ return false ;
50+ }
51+ loginStatusLoaded = true ;
52+ return true ;
53+ } )
54+ . catch ( ( ) => {
55+ clearLoginStatus ( ) ;
56+ return false ;
57+ } )
58+ . finally ( ( ) => {
59+ loginStatusLoading = null ;
60+ } ) ;
61+ }
62+ return loginStatusLoading ;
63+ } ;
1464
1565const loadXpackEEStatus = async ( ) => {
66+ if ( xpackEEStatusLoaded ) {
67+ return GlobalStore ( ) . isXpackEE ;
68+ }
1669 if ( ! xpackEELoading ) {
1770 xpackEELoading = fetch ( '/api/v2/core/auth/setting' , {
1871 credentials : 'include' ,
@@ -21,6 +74,7 @@ const loadXpackEEStatus = async () => {
2174 . then ( ( res ) => {
2275 const globalStore = GlobalStore ( ) ;
2376 globalStore . isXpackEE = ! ! res ?. data ?. isXpackEE ;
77+ xpackEEStatusLoaded = true ;
2478 return globalStore . isXpackEE ;
2579 } )
2680 . catch ( ( ) => GlobalStore ( ) . isXpackEE )
@@ -32,6 +86,9 @@ const loadXpackEEStatus = async () => {
3286} ;
3387
3488const loadXpackEELicenseStatus = async ( ) => {
89+ if ( licenseStatusLoaded ) {
90+ return GlobalStore ( ) . isXpackEELicensed ;
91+ }
3592 if ( ! licenseStatusLoading ) {
3693 licenseStatusLoading = fetch ( '/api/v2/core/xpackee/licenses/status' , {
3794 credentials : 'include' ,
@@ -40,7 +97,13 @@ const loadXpackEELicenseStatus = async () => {
4097 } ,
4198 } )
4299 . then ( ( res ) => res . json ( ) )
43- . then ( ( res ) => res ?. data ?. status === 'Bound' )
100+ . then ( ( res ) => {
101+ const globalStore = GlobalStore ( ) ;
102+ const licensed = res ?. data ?. status === 'Bound' ;
103+ globalStore . isXpackEELicensed = licensed ;
104+ licenseStatusLoaded = true ;
105+ return licensed ;
106+ } )
44107 . catch ( ( ) => false )
45108 . finally ( ( ) => {
46109 licenseStatusLoading = null ;
@@ -53,7 +116,21 @@ router.beforeEach(async (to, from, next) => {
53116 NProgress . start ( ) ;
54117 axiosCanceler . removeAllPending ( ) ;
55118 const globalStore = GlobalStore ( ) ;
56- if ( to . name !== 'entrance' && to . name !== 'XpackEELicenseRequired' && ! globalStore . isLogin ) {
119+ if ( globalStore . isLogin ) {
120+ const loggedIn = await loadLoginStatus ( ) ;
121+ if ( ! loggedIn ) {
122+ if ( to . name !== 'entrance' ) {
123+ next ( {
124+ name : 'entrance' ,
125+ params : to . params ,
126+ } ) ;
127+ NProgress . done ( ) ;
128+ return ;
129+ }
130+ return next ( ) ;
131+ }
132+ }
133+ if ( to . name !== 'entrance' && ! globalStore . isLogin ) {
57134 next ( {
58135 name : 'entrance' ,
59136 params : to . params ,
@@ -76,15 +153,8 @@ router.beforeEach(async (to, from, next) => {
76153 if ( globalStore . isLogin && to . name !== 'login' ) {
77154 await loadXpackEEStatus ( ) ;
78155 }
79- if (
80- globalStore . isLogin &&
81- globalStore . isXpackEE &&
82- to . name !== 'XpackEELicenseRequired' &&
83- to . name !== 'entrance' &&
84- to . name !== 'login'
85- ) {
86- const licensed = globalStore . isXpackEELicensed || ( await loadXpackEELicenseStatus ( ) ) ;
87- globalStore . isXpackEELicensed = licensed ;
156+ if ( globalStore . isLogin && globalStore . isXpackEE && ! xpackEELicenseCheckWhiteList . includes ( String ( to . name ) ) ) {
157+ const licensed = await loadXpackEELicenseStatus ( ) ;
88158 if ( ! licensed ) {
89159 next ( { name : 'XpackEELicenseRequired' , query : { code : String ( to . params . code || '' ) } } ) ;
90160 NProgress . done ( ) ;
@@ -100,7 +170,8 @@ router.beforeEach(async (to, from, next) => {
100170 NProgress . done ( ) ;
101171 return ;
102172 }
103- if ( globalStore . isXpackEELicensed ) {
173+ const licensed = await loadXpackEELicenseStatus ( ) ;
174+ if ( licensed ) {
104175 next ( { name : 'home' } ) ;
105176 NProgress . done ( ) ;
106177 return ;
0 commit comments