Ad
Cannot Check If An User Is Loged In Firebase + Angular
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.
Ad
Answer
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;
}
}));
}
Ad
source: stackoverflow.com
Related Questions
- → Make a Laravel collection into angular array (octobercms)
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → Angularjs not working inside laravel form
- → Analysis of Flux implementations
- → how to write react component to construct HTML DOM
- → angular ng-repeat and images in a row
- → Conditional BG Color on AngularJS
- → Should I 'use strict' for every single javascript function I write?
- → getting the correct record in Angular with a json feed and passed data
- → "Undefined is not a function" at .toBe fucntion
- → angularjs data binding issue
- → Angular / JavaScript auto hydrate an instance
- → Convert generic text string in json object into valid url using Angular
Ad