当用户通过Firebase Google登录身份验证登录Angular Application时,存储用户电子邮件时遇到问题

时间:2019-01-03 11:00:38

标签: angular angular5 angular6 angularfire2

我正在登录页面上使用ANGULAR Application。

我正在使用Firebase在我的应用程序中实现Google登录身份验证。我已经实施了Google登录:

  • 但是我在存储通过我的应用程序登录的用户电子邮件时遇到问题。

我正在共享 component.ts 的代码:

import { Component, OnInit } from '@angular/core';
import { FormGroup  , FormControl  , Validators } from '@angular/forms';
import {Router} from '@angular/router';
import { ServicesService } from '../service/services.service';
import * as firebase from 'firebase';
import { AngularFireAuth } from 'angularfire2/auth';
import { auth } from 'firebase/app';    

    @Component({
      selector: 'app-login',
      templateUrl: './login.component.html',
      styleUrls: ['./login.component.scss']
    })
    export class LoginComponent implements OnInit {

     constructor( public router: Router ,  public restapi: ServicesService  , private afAuth: AngularFireAuth  ) {

      googlelogin() {
    var user = firebase.auth().currentUser;
var name, email, photoUrl, uid, emailVerified;
    alert('hello');
    return this.afAuth.auth.signInWithRedirect(new firebase.auth.GoogleAuthProvider());
    if (user != null) {
      name = user.displayName;
      email = user.email;
      photoUrl = user.photoURL;
      emailVerified = user.emailVerified;
      uid = user.uid;
      console.log(email);
    }
  }

Component.html

 <button mat-button (click)='googlelogin()' class=""> Sign in With Google </button>

1 个答案:

答案 0 :(得分:0)

嗨阿努拉格,欢迎您来。您的代码:

if (user != null) {
    name = user.displayName;
    email = user.email;
    photoUrl = user.photoURL;
    emailVerified = user.emailVerified;
    uid = user.uid;
    console.log(email);
}

不会运行,因为它在return语句之后...但是,.signInWithRedirect()方法返回一个Promise,因此您可以对.then()和{{ 1}}。

.catch()

我建议您签出this link or this updated one for v5 ),它会逐步指导您设置第三方身份验证。整个AngularFirebase网站都非常适合学习Angular和Firebase内容。