11import axios from 'axios';
22import { Store } from 'vuex';
3- import VueRouter from 'vue-router';
43<% _ if (enableTranslation) { _% >
54import TranslationService from ' @/locale/translation.service' ;
65< % _ } _%>
76
87export default class AccountService {
9-
108 constructor(
119 private store: Store<any >,
1210<% _ if (enableTranslation) { _% >
@@ -15,99 +13,89 @@ export default class AccountService {
1513<% _ if (authenticationTypeSession || authenticationTypeOauth2) { _% >
1614 private cookie: any,
1715< % _ } _%>
18- private router: VueRouter
1916 ) {
20- this.init();
2117 }
2218
23- public init(): void {
24- this.retrieveProfiles();
19+ public async init(): Promise<void > {
20+ if (!this.store.getters.profilesLoaded) {
21+ await this.retrieveProfiles();
22+ this.store.commit('profilesLoaded');
23+ }
2524 }
2625
27- public retrieveProfiles(): Promise<boolean > {
28- return new Promise(resolve => {
29- axios.get<any >('management/info').then(res => {
30- if (res.data && res.data.activeProfiles) {
31- this.store.commit('setRibbonOnProfiles', res.data['display-ribbon-on-profiles']);
32- this.store.commit('setActiveProfiles', res.data['activeProfiles']);
33- }
34- resolve(true);
35- }).catch(() => resolve(false));
36- });
26+ public async retrieveProfiles(): Promise<boolean > {
27+ try {
28+ const res = await axios.get<any >('management/info');
29+ if (res.data && res.data.activeProfiles) {
30+ this.store.commit('setRibbonOnProfiles', res.data['display-ribbon-on-profiles']);
31+ this.store.commit('setActiveProfiles', res.data['activeProfiles']);
32+ }
33+ return true;
34+ } catch (error) {
35+ return false;
36+ }
3737 }
3838
39- public retrieveAccount(): Promise<boolean > {
40- return new Promise(resolve => {
41- axios
42- .get<any >('api/account').then((response) => {
43- this.store.commit('authenticate');
39+ public async retrieveAccount(): Promise<boolean > {
40+ try {
41+ const response = await axios.get<any >('api/account');
42+ if (response.status === 200 && response.data) {
4443 const account = response.data;
45- if (account) {
46- this.store.commit('authenticated', account);
44+ this.store.commit('authenticated', account);
4745<% _ if (enableTranslation) { _% >
48- if (this .store .getters .currentLanguage !== account .langKey ) {
49- this .store .commit (' currentLanguage' , account .langKey );
50- }
51- < % _ } _%>
52- if(sessionStorage.getItem('requested-url')) {
53- this.router.replace(sessionStorage.getItem("requested-url"));
54- sessionStorage.removeItem("requested-url");
55- }
56- } else {
57- this.store.commit('logout');
58- if (this.router.currentRoute.path !== '/') {
59- this.router.push('/');
60- }
61- sessionStorage.removeItem("requested-url");
46+ if (this .store .getters .currentLanguage !== account .langKey ) {
47+ this .store .commit (' currentLanguage' , account .langKey );
48+ this .translationService .refreshTranslation (this .store .getters .currentLanguage );
6249 }
63- <% _ if (enableTranslation) { _% >
64- this .translationService .refreshTranslation (this .store .getters .currentLanguage );
6550< % _ } _%>
66- resolve( true) ;
67- }).catch(() => {
68- this.store.commit('logout');
69- resolve(false);
70- } );
71- }) ;
51+ return true;
52+ }
53+ } catch (error) {}
54+
55+ this.store.commit('logout' );
56+ return false ;
7257 }
7358
74- public hasAnyAuthorityAndCheckAuth(authorities: any): Promise< boolean > {
75- if (typeof authorities === 'string' ) {
76- authorities = [authorities] ;
59+ public async loadAccount() {
60+ if (this.store.getters.logon ) {
61+ return this.store.getters.logon ;
7762 }
7863
79- if (!this.authenticated || !this.userAuthorities) {
80- const token = <% _ if (authenticationTypeJwt) { _% > localStorage .getItem (' <%=jhiPrefixDashed %>-authenticationToken' ) || sessionStorage .getItem (' <%=jhiPrefixDashed %>-authenticationToken' ); < % _ } else { _% > this .cookie .get (' JSESSIONID' ) || this .cookie .get (' XSRF-TOKEN' ); < % _ } _%>
81- if (!this.store.getters.account && !this.store.getters.logon && token) {
82- return this.retrieveAccount().then(resp => {
83- if (resp) {
84- return this.checkAuthorities(authorities);
85- }
86- return Promise.resolve(false);
87- });
88- }
89- return Promise.resolve(false);
64+ const token = localStorage.getItem('jhi-authenticationToken') || sessionStorage.getItem('jhi-authenticationToken');
65+ if (this.authenticated && this.userAuthorities && token) {
66+ return;
67+ }
68+
69+ const promise = this.retrieveAccount();
70+ this.store.commit('authenticate', promise);
71+ return await promise;
72+ }
73+
74+ public async hasAnyAuthorityAndCheckAuth(authorities: any): Promise<boolean > {
75+ if (typeof authorities === 'string') {
76+ authorities = [authorities];
9077 }
9178
79+ await this.loadAccount();
9280 return this.checkAuthorities(authorities);
9381 }
9482
9583 public get authenticated(): boolean {
9684 return this.store.getters.authenticated;
9785 }
9886
99- public get userAuthorities(): any {
87+ public get userAuthorities(): string[] {
10088 return this.store.getters.account?.authorities;
10189 }
10290
103- private checkAuthorities(authorities: any ): Promise< boolean > {
91+ private checkAuthorities(authorities: string[] ): boolean {
10492 if (this.userAuthorities) {
10593 for (const authority of authorities) {
10694 if (this.userAuthorities.includes(authority)) {
107- return Promise.resolve( true) ;
95+ return true;
10896 }
10997 }
11098 }
111- return Promise.resolve( false) ;
99+ return false;
112100 }
113101}
0 commit comments