如何在angular和firebase中使用Gmail对电子邮件进行身份验证?

时间:2019-11-22 21:31:03

标签: javascript angular firebase firebase-authentication angularfire

我想让电子邮件在angular和firebase中对gmail进行身份验证,所以我创建了功能

one_read['user']['place']['country']

auth-client.service.ts

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

我将电子邮件身份验证分配给navbar.component.ts(userLoggedIn)中的一个变量。

navbar.component.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));
  }
}

最后我想在navbar.component.html中显示它,但要显示rine。 navbar.component.html

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;

       }
    })

  }
}

1 个答案:

答案 0 :(得分:2)

更改此:

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

       }
    })

  }

对此:

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

  }