Currently I login my user and then I want to implement some route Guards in order for them to not be accessible if the user is not logged in.
SO this is my guard.
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';
import { AngularFireAuth } from '@angular/fire/auth';
@Injectable({
providedIn: 'root'
})
export class AuthguardGuard implements CanActivate {
constructor(public af: AngularFireAuth){
}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
var user = this.af.auth().currentUser;
if (user) {
return true;
} else {
return false;
}
}
}
Problem is that ---> var user = this.af.auth().currentUser;
is giving an error
Cannot invoke an expression whose type lacks a call signature. Type 'Auth' has no compatible call signatures.ts(2349)
(property) AngularFireAuth.auth: firebase.auth.Auth
oO how am I supposed to check if the user is logged in? I don't want to use localSTorage for that.
I ended up using the following canActivate
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.auth.authState.pipe(map(User => {
if (User) {
return true;
} else {
this.router.navigate(['/login']);
return false;
}
}));
}