Search code examples
javascriptangularfirebasefirebase-authenticationangularfire

how to get email authenticate with Gmail in angular and firebase?


i want to get email authenticate gmail in angular and firebase, i created the function

getAuth (){ return this.afAuth.authState.pipe (map (auth => auth));} .

auth-client.service.ts

import { map } from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase/app'

@Injectable({
  providedIn: 'root' 
})

export class AuthClientService {

constructor(private afAuth:AngularFireAuth) { }

 getAuth(){
    return this.afAuth.authState.pipe(map(auth=>auth));
  }
}

i assign email authenticate to a variable in navbar.component.ts (userLoggedIn).

navbar.component.ts

import { Router } from '@angular/router';
import { FlashMessagesService } from 'angular2-flash-messages';
import { AuthClientService } from './../../services/auth-client.service';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.css']
})
export class NavbarComponent implements OnInit {
  isLoggedIn:boolean=false;
  userLoggedIn:string;

  constructor(
    private authService:AuthClientService,
    private flashMessage:FlashMessagesService,
    private route:Router
    ) { }
 ngOnInit() {
    this.authService.getAuth().subscribe(auth=>{
      if(auth){
       return this.isLoggedIn=true;
       this.userLoggedIn  = auth.email; 
       }else{
         this.isLoggedIn=false;

       }
    })

  }
}

in the end I want to display it in navbar.component.html but rine to display. navbar.component.html

<li class="nav-item">
          <a routerLink="/login" class="nav-link">{{ userLoggedIn }} </a>
      </li>

Solution

  • Change this:

     ngOnInit() {
        this.authService.getAuth().subscribe(auth=>{
          if(auth){
           return this.isLoggedIn=true;
           this.userLoggedIn  = auth.email; 
           }else{
             this.isLoggedIn=false;
    
           }
        })
    
      }
    

    into this:

     ngOnInit() {
        this.authService.getAuth().subscribe(auth=>{
          if(auth){
            this.userLoggedIn  = auth.email;
            this.isLoggedIn = true; 
           }else{
             this.isLoggedIn = false;
           }
        })
    
      }