Answer the question
In order to leave comments, you need to log in
How to correctly check authorization when initializing an application (Angular2)?
Good day,
During the initialization of the application, I make a request to the backend to get the navigation menu (different depending on the role)
providers: [
provide: APP_INITIALIZER,
useFactory: (service: GlobalService) => () => service.init(),
deps: [GlobalService], multi: true,
]
init(): Promise<any> {
var promise = this.http.get('/api/menu_navigation').map(res => res.json()).toPromise();
promise.then(data => this.menuNavigation = data.menu_navigation);
return promise;
}
Answer the question
In order to leave comments, you need to log in
Strange, apparently somewhere the first time he made a mistake.
In general, who will be interested or can help someone. In init, we simply write a condition, something like:
And then all the redirection work is done by routing, immediately after the application is fully initialized.
The isLoggedIn () method is a normal return of a private property, which is defined as follows:
set loggedIn(val: boolean) {
if (typeof val == 'undefined') {
val = !!this.cookie.getObject('loggedIn');
} else {
this.cookie.putObject('loggedIn', val);
}
this._loggedIn = val;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question