return user.uid onAuthStateChanged

瘦欲@ 提交于 2019-12-12 06:36:49

问题


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

I want to function return uid like this: ewfwe3423sdfsdakmasd


回答1:


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

Refs: Here and here



来源:https://stackoverflow.com/questions/44795636/return-user-uid-onauthstatechanged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!