i have a login page and 3 more pages along with that. Now i need to do authentication function. If the authentication is undefined then it must redirected to login page, if authentication is true it must go to checkLogin function. I am not getting how to do this but i had tried in this way,in one of the pages.
ts:
import { Router, CanActivate } from '@angular/router';
@Injectable()
export class PageComponent implements CanActivate {
constructor(private router: Router) { }
canActivate() {
if (localStorage.getItem('authToken') == undefined ) {
this.router.navigate(['/login']);
}
else {
/*Checklogin*/
this.ApiService
.checklogin()
.subscribe(
user => {}
}
}
but i get an error:
Class 'PageComponent' incorrectly implements interface 'CanActivate'.
Types of property 'canActivate' are incompatible.
Type '() => void' is not assignable to type '(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => boolean | Observable<boolean> | Pr...'.
Type 'void' is not assignable to type 'boolean | Observable<boolean> | Promise<boolean>'.
I dont know what i am doing is correct or not, can anyone guide me