【问题标题】:Retrieving UID's from Firebase in ANgular在 Angular 中从 Firebase 中检索 UID
【发布时间】:2020-03-14 06:56:50
【问题描述】:

我在我的 Angular 项目中使用简单的电子邮件/密码和谷歌登录 (firebase) 登录身份验证。我想检索当前登录产品的用户的用户 ID (UID)。有没有角度提供检索 UID 的方法?

this.authenticationService.SignUp(this.institutes.email,"123456");
    console.log(this.af.auth.currentUser.uid);
身份验证脚本:

SignUp(email: string, password: string) {
this.angularFireAuth
.auth
.createUserWithEmailAndPassword(email, password)
.then(res => {
console.log('You are Successfully signed up!', res);
this.angularFireAuth
    .auth
    .signInWithEmailAndPassword(email, password)
    .then(res => {
    console.log('You are Successfully logged in!');
    return 1;
    })
    .catch(err => {
    console.log('Something is wrong:',err.message);
    });
})
.catch(error => {
console.log('Something is wrong:', error.message);
});
}

我已经注册了一个新帐户并同时登录了该用户。登录后我尝试检索用户的 UID,但收到以下错误: 错误:对象可能为“空”。 错误在线:console.log(this.af.auth.currentUser.uid);

【问题讨论】:

    标签: javascript angular firebase firebase-authentication


    【解决方案1】:

    要检索 uid,您可以使用 AngularFire 并执行以下操作:

    export class AppComponent {
      constructor(public afAuth: AngularFireAuth) {
      }
      getUid() {
        this.afAuth.auth.currentUser.uid;
      }
    }
    

    您可以在这里查看更多信息:

    https://github.com/angular/angularfire/blob/master/docs/auth/getting-started.md

    【讨论】:

    • 我们是否可以手动传递一个email id来获取它对应的UID?
    【解决方案2】:

    Peter 回答了在使用 AngularFire2 时如何获取登录用户的 UID。如果你没有使用 AngularFire(或者即使你使用了),你可以通过以下方式获取当前用户的 UID:

    firebase.auth().currentUser.uid
    

    【讨论】:

    • 我们是否可以手动传递一个email id来获取它对应的UID?
    • 不。该功能在客户端 SDK 中不可用。见stackoverflow.com/…
    • 我已经附加了我试图检索 UID 但仍然未能获得它的代码。请去看看。
    【解决方案3】:

    这是您在 2021 年底检索用户 ID 的方法。

    import { AngularFireAuth }           from "@angular/fire/auth";
    
    constructor
       // paramenters
    (
       public angularFireAuth: AngularFireAuth
    
    )
       // code block
    {
    }
    
    this.angularFireAuth.currentUser.then( data  => {
      console.log(data);
      console.log(data.uid);
    )}
    

    package.json

    "firebase": "^7.0 || ^8.0",
    "@angular/fire": "^6.1.5"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      相关资源
      最近更新 更多