i have working angular2 guard canactivate()
calls service isloggedin()
, returns promise resolves , route handled appropiately.
however, trying opposite, see when user not logged in, , isn't working.
i tried simple (adding ! operator), hoping work:
@injectable() export class authguard implements canactivate { constructor(private authservice: authservice) {} canactivate() { return !this.authservice.isloggedin(); } }
however, returns falsey value , route never activates.
this relevant excerpt of isloggedin()
function:
isloggedin(): promise<boolean> { var component = this; return new promise((resolve, reject) => { component.queryforuser((user) => { resolve(user != null); }); } }); }
if user not equal null
, logged in , promise resolves true. else, false.
while add parameter specify state looking for, or create isnotloggedin()
function, same logic inverted, ask, there way negate value of promise's resolve canactivate()
?
return !this.authservice.isloggedin()
won't work, because of how js works. this.authservice.isloggedin()
promise object , truthy. !this.authservice.isloggedin()
false.
instead, promise result should mapped negated result with
canactivate() { return this.authservice.isloggedin().then(result => !result); }
or
async canactivate() { return !(await this.authservice.isloggedin()); }
the parentheses around await ...
optional , used readability.
Comments
Post a Comment