Reputation: 89
In my Angular-Firebase online shopping project I used AuthGuard
to check whether user is logged in or not to access ./check-out
and other links as shown in code below. Getting an error as the map
doesn't get imported for Observable.User
in code below. Using everything of latest version.
auth.service.ts
import { Observable } from 'rxjs';
import { Injectable } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/auth';
import * as firebase from 'firebase';
@Injectable({
providedIn: 'root'
})
export class AuthService {
user$: Observable<firebase.User>;
constructor(private afAuth: AngularFireAuth) {
this.user$=afAuth.authState;
}
login(){
this.afAuth.auth.signInWithRedirect(new firebase.auth.GoogleAuthProvider())
}
logout(){
this.afAuth.auth.signOut()
}
}
login.component.ts
import { AuthService } from './../auth.service'
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent{
constructor(private auth: AuthService) { }
ngOnInit() {
}
login(){
this.auth.login();
}
}
bs-navbar.component.ts
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../auth.service';
@Component({
selector: 'bs-navbar',
templateUrl: './bs-navbar.component.html',
styleUrls: ['./bs-navbar.component.css']
})
export class BsNavbarComponent{
constructor(public auth: AuthService) {
}
logout(){
this.auth.logout();
}
}
bs-navbar.component.html
<nav class="navbar navbar-expand-md navbar-light bg-light fixed-top">
<a class="navbar-brand" routerLink="/">O</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" routerLink="/shopping-cart">Shopping cart</a>
</li>
<ng-template #anonymousUser>
<li class="nav-item">
<a class="nav-link" routerLink="/login">Login</a>
</li>
</ng-template>
<li ngbDropdown *ngIf="auth.user$ | async as user; else anonymousUser" class="nav-item dropdown">
<a ngbDropdownToggle class="nav-link dropdown-toggle" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{user.displayName}}
</a>
<div ngbDropdownMenu class="dropdown-menu" aria-labelledby="dropdown01">
<a class="dropdown-item" routerLink="/my/orders">My Orders</a>
<a class="dropdown-item" routerLink="/admin/orders">Manage Orders</a>
<a class="dropdown-item" routerLink="/admin/products">Manage Prodcuts</a>
<a class="dropdown-item" (click)="logout()">Log Out</a>
</div>
</li>
</ul>
</div>
</nav>
auth.guard.service.ts
import { Injectable } from '@angular/core';
import { AuthService } from './auth.service';
import { Router } from '@angular/router';
import { CanActivate } from '@angular/router';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class AuthGuardService implements CanActivate{
constructor(private auth:AuthService, private router: Router) {
}
canActivate(): boolean{
return this.auth.user$.map(user=>{
if(user) return true;
this.router.navigate(['./login']);
return false;
});
}
}
Getting error in auth.guard.service.ts in return this.auth.user$.map(user=>
line.
Upvotes: 0
Views: 451
Reputation: 5876
If you're using the newest version of RxJS, it is probably because the map
operator must be piped like this:
return this.auth.user$.pipe(map(user => ...));
You can see an example of this in here :https://www.learnrxjs.io/operators/transformation/map.html
Upvotes: 6