访问firebase.auth()typescript中的全局变量

时间:2018-03-02 14:56:05

标签: angular typescript

我在外面宣布了一个可变的user。但是我无法在firebase.auth().signInWithPopup(provider).then((result) => {内访问this.user。它显示未定义。

    import { ...everything } from 'everything';    

    @Component({
      selector: 'page-login',
      templateUrl: 'login.html',
    })
interface User{
  email: string;
  displayName: string;
  uid?: string;
  photoUrl?: string;
}
    export class LoginPage {

      user:User;

      constructor(...every) {
      }

      validateForm(form){
        if(form.valid){
            var provider = new firebase.auth.GoogleAuthProvider();
            provider.addScope('https://www.googleapis.com/auth/plus.login');
            firebase.auth().signInWithPopup(provider).then((result) => {
                const token = result.credential.accessToken;
                const userData = result.user;
                this.user.email = userData.email;    
            }).catch(function(error) {
            });
          }
        }
      }
    }

1 个答案:

答案 0 :(得分:0)

import { Injectable } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/auth';

export class LocalAuthService {

  constructor(private afAuth: AngularFireAuth) { }

  userID: string;
  userName: string;

  public signUp(email: string, password: string) {

    console.log('The service method has been called');
      //This will signup your new user. Make sure signup with email is enabled
      this.afAuth.auth.createUserWithEmailAndPassword(email, password).catch( function(error) {

        console.log('The Error that has occurred is' + error.message);
        console.log('The Error that has occurred with error code ' + error.code);

      });

      this.afAuth.auth.onAuthStateChanged( function(user) {
        if (
            this.userID = user.uid;
            this.userName = user.displayName;

        }
      });

  }