I tried to insert an authentication system in the Angular app. (This one.
After finishing everything, I got for this code:
private userCurrentState = new BehaviorSubject<boolean>(this.tokenAuthService.isSignedin());
this error message:
Argument of type 'boolean | undefined' is not assignable to parameter of type 'boolean'.
TokenAuthService
codegetJwtToken() {
return localStorage.getItem('auth_token');
}
validateToken() {
const token = this.getJwtToken();
if(token) {
const payload = this.payload(token);
if(payload) {
return Object.values(this.tokenIssuer).indexOf(payload.iss) > -1 ? true : false;
}
}
else {
return false;
}
}
isSignedin() {
return this.validateToken();
}
How can I fix?
OK, maybe the previous answers will do the work, but they didn't explain why your function validateToken's return value results in undefined.
validateToken(){
const token = this.getJwtToken();
if(token){
const payload = this.payload(token);
if(payload){
return Object.values(this.tokenIssuer).indexOf(payload.iss) > -1 ? true : false;
}
// You will get undefined here if payload is a falsy value
// and thus you are getting <boolean|undefined> as result type.
// One option is to provide else when !payload
} else {
return false;
}
}