【问题标题】:how to get email authenticate with Gmail in angular and firebase?如何在 Angular 和 Firebase 中使用 Gmail 进行电子邮件身份验证?
【发布时间】:2019-11-22 21:31:03
【问题描述】:

我想在 Angular 和 Firebase 中获取电子邮件身份验证 gmail,我创建了函数

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));
  }
}

我将电子邮件身份验证分配给 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;

       }
    })

  }
}

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

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

【问题讨论】:

  • 你能和我们分享github上的存储库吗?谢谢
  • @ZINEMahmoud oki

标签: javascript angular firebase firebase-authentication angularfire


【解决方案1】:

改变这个:

 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;
       }
    })

  }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-03-24
  • 2019-06-11
  • 2018-09-03
  • 2018-09-22
  • 1970-01-01
  • 2017-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多