简体   繁体   English

在firebase.auth()类型脚本中访问全局变量

[英]accessing global variables in firebase.auth() typescript

I have declare a vaiable user outside. 我已经在外面声明了一名可变user But I am unable to access this.user inside firebase.auth().signInWithPopup(provider).then((result) => { . It is showing undefined. 但是我无法在firebase.auth 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) {
            });
          }
        }
      }
    }
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;

        }
      });

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 firebase.auth()。currentUser为null - firebase.auth().currentUser is null 如何将 firebase.auth 导入到 angular 10 - how to import firebase.auth to angular 10 firebase.auth()。onAuthStateChanged返回什么? - what is returned on firebase.auth().onAuthStateChanged? 更新 Firebase 和 Angular 后获取“firebase.auth is not a function” - Getting “firebase.auth is not a function” after updating Firebase and Angular 如何从firebase.auth()。onAuthStateChanged中提取数据 - How to extract the data from a firebase.auth().onAuthStateChanged firebase.auth()。当刷新页面时,currentUser返回null - firebase.auth().currentUser returns null when page is refreshed Firebase:如何从 firebase.auth().onAuthStateChanged 异步绑定用户到 Angular 模板? - Firebase: How to asynchronously bind User from firebase.auth().onAuthStateChanged to Angular template? 使用Firebase,即使正在记录firebase.auth()。currentUser,它也是一个空值 - Using Firebase, firebase.auth().currentUser is a null value even though it is being logged 全局变量打字稿未定义 - global variables typescript not defined 为什么firebase.auth().currentUser即使用户登录也返回NULL - Why does firebase.auth().currentUser return NULL even when the user is logged in
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM