I have been working on creating a login authorization system to secure certain routes in an angular application, but I keep encountering a TypeScript error in the auth-guard.service during compilation. Despite my efforts, I am unable to pinpoint the issue. Below is the code for auth-guard.service:
import { CanActivate,
ActivatedRoute,
RouterStateSnapshot,
ActivatedRouteSnapshot,
Router
} from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { Injectable } from '@angular/core';
import { AuthService } from './auth.service';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router ) {}
canActivate(route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.authService.isAuthenticated()
.then(
(authenticated: boolean) => {
if (authenticated) {
return true;
} else {
this.router.navigate(['/']);
return false;
}
}
);
}
}
The error message I'm seeing reads as follows:
ERROR in src/app/auth-guard.service.ts(19,26): error TS2339: Property 'then' does not exist on type 'void'.
Here is the content of my auth-service.ts:
export class AuthService {
loggedIn = false;
isAuthenticated() {
const promise = new Promise (
(resolve, reject) => {
setTimeout(() => {
resolve(this.loggedIn);
}, 800);
}
);
}
login() {
this.loggedIn = true;
}
logout() {
this.loggedIn = false;
}
}
Do I need to define a return value for isAuthenticated? What could be causing this issue?
Thank you in advance for any assistance...