返回 user.uid onAuthStateChanged [英] return user.uid onAuthStateChanged

查看:22
本文介绍了返回 user.uid onAuthStateChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小函数,当我在控制台中查看时它可以工作:在控制台中返回 user.uid 但我想返回 uid 以在其他函数中使用

I have small function and it works when I look in console: returns user.uid in console but I want to return uid to use in other function

newUserUid() {
return firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
   console.log(user.uid);
  }
});
}

我想像这样返回uid:ewfwe3423sdfsdakmasd

I want to function return uid like this: ewfwe3423sdfsdakmasd

推荐答案

您需要订阅到 auth 状态.您在 Angular 2 应用中使用 JavaScript SDK,请改用 AngularFire2 SDK.

You need to subscribe to the auth state. You are using the JavaScript SDK inside an Angular 2 app, use the AngularFire2 SDK instead.

import { AngularFireAuth } from 'angularfire2/auth';
...
constructor(public af: AngularFireAuth) {}
...

someOtherFunction() {
    newUserUid().subscribe(auth => {
        // Now use value 
        if (auth) {
            console.log(auth.uid);
        } else {
           // logged out
        }
    });
}

newUserUid() {
    return this.af.auth;
}

参考:这里这里

这篇关于返回 user.uid onAuthStateChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆