In my online shopping project that combines Angular and Firebase, I implemented the AuthGuard
to verify user login status before accessing various links including ./check-out
. However, I encountered an issue with importing map
for Observable.User
. All components are using the latest versions.
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;
});
}
}
Encountering an error in auth.guard.service.ts on the line
return this.auth.user$.map(user=>
.