Hi,
I have an AuthGard, which redirects to a login screen, if login is
necessary and the user did not yet log in.
@Injectable()
export class AuthGuard implements CanActivate {
authenticationService: AuthenticationService;
constructor(private router: Router, authenticationService:
AuthenticationService) {
this.authenticationService = authenticationService
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (! this.authenticationService.isLoginNecessary() || this.
authenticationService.isUserLoggedIn()) {
return true;
}
else {
// not logged in so redirect to login page with the return url
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
console.log("AuthGuard noticed user login is necessary and user is not
logged in, redirect to login page")
return false;
}
}
}
The authenticationService.isLoginNecessary() gets it's information from an
server call generated via swagger service.
As you see in
@Injectable()
export class AuthenticationService {
configservice: ConfigurationService;
configInfo: ConfigInfo;
constructor(configservice:ConfigurationService) {
this.configservice = configservice
}
ngOnInit () {
console.log ("ngOnInit of AutenticationServiec called - yeah")
this.configservice.getConfiguration().subscribe (
(x) => {
this.configInfo = x;
},
(e) => console.log(e),
() => console.log('Get configuration completed (' + this.configInfo + ')')
)
}
isLoginNecessary () : boolean {
return this.configInfo.loginNeeded
}
I have located fetching the info in the ngInit() of authenticationService,
but in isLoginNecessary() the configInfo is not set yet, so getting an
exception.
I think the problem is configservice is generated asynchronous.
Can you please give me advice how I can ensure that this.configInfo is
initialized when isLoginNecessary() is called?
Thanks in advance
--
You received this message because you are subscribed to the Google Groups
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.