在 $onAuthStateChanged 上找到正确的 Firebase Auth id_token [英] Finding correct Firebase Auth id_token on $onAuthStateChanged

查看:27
本文介绍了在 $onAuthStateChanged 上找到正确的 Firebase Auth id_token的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用 Angular 1.5.8Firebase 3.3.0AngularFire 2.0.2.当我调用 $firebaseAuth.$signInWithPopup 时,promise 返回一个包含 firebase.user.idToken 的 firebase 用户,但是当我调用 $onAuthStateChanged 时>,firebase 用户不会返回 .user 属性.$onAuthStateChanged 函数中用于服务器身份验证的正确令牌是什么?

I am currently using Angular 1.5.8, Firebase 3.3.0, and AngularFire 2.0.2. When I call $firebaseAuth.$signInWithPopup, the promise returns with a firebase user that includes firebase.user.idToken, but when I call $onAuthStateChanged, firebase user does not return with a .user property. What is the correct token to use for server authentication from the $onAuthStateChanged function?

我使用了 md 属性,但后来它停止工作并切换到 kd 属性,我意识到并不是每个用户都有.它是 _lat 属性吗?它似乎是这样工作的,但我似乎找不到任何关于它的文档.或者这不是在身份验证状态更改上使用令牌的正确方法?有最佳实践吗?这是我从承诺返回的对象.

I was using the md property, but then it stopped working and switched to a kd property and I realized that not every user has that. Is it the _lat property? It seems to work this way, but I can't seem to find any documentation around it. Or is this not the correct way to be using the token on an Auth State Change? Is there a best practice? Here is the object I have being returned from the promise.

我为图像道歉,但奇怪的是,如果我 JSON.stringify(firebaseUser) 并打印它,我最终会得到一个完全不同的对象,其中 firebaseUser.stsTokenManager.accessToken 会给我是正确的密钥,但如果我尝试 firebaseUser.stsTokenManager.accessToken 它说 stsTokenManager 未定义.

I appologize for the image, but what is strange is that if I JSON.stringify(firebaseUser) and print it, I end up with a completely different object where firebaseUser.stsTokenManager.accessToken would give me the right key, but if I try firebaseUser.stsTokenManager.accessToken it says that stsTokenManager is undefined.

{
  "uid": "XXXXXXXXXXXXXXXXXXXXXXXXXX",
  "displayName": "Luke Schlangen",
  "photoURL": "XXXXXXXXXXXXXXXXXXXXXXXXXX",
  "email": "XXXXXXXXXXXXXXXXXX@XXXXX.com",
  "emailVerified": true,
  "isAnonymous": false,
  "providerData": [
    {
      "uid": "XXXXXXXXXXXXXXXXXXXXXXXXXX",
      "displayName": "Luke Schlangen",
      "photoURL": "XXXXXXXXXXXXXXXXXXXXXXXXXX",
      "email": "XXXXXXXXXXXXXXXXXX@XXXXX.com",
      "providerId": "google.com"
    }
  ],
  "apiKey": "XXXXXXXXXXXXXXXXXXXXXXXXXX",
  "appName": "[DEFAULT]",
  "authDomain": "XXXXXXXXXXXXXXXXXXXXXXXXXX",
  "stsTokenManager": {
    "apiKey": "XXXXXXXXXXXXXXXXXXXXXXXXXX",
    "refreshToken": "XXXXXXXXXXXXXXXXXXXXXXXXXX",
    "accessToken": "XXXXXXXXXXXXXXXXXXXXXXXXXX",
    "expirationTime": XXXXXXXXXXXXXXXXXXXXXXXXXX
  },
  "redirectEventId": null
}

所有代码现在看起来都可以正常工作,并且似乎对每个用户都有效,但我想知道,是否有最佳实践?我应该使用 JSON 进行转换,然后从该对象中选择属性吗?或者 _lat 是获取此密钥的正确方法吗?

All of the code seems functional now and seems to be working for every user, but I'm wondering, is there a best-practice for this? Should I be using JSON to convert and then selecting the property from that object? Or is _lat the proper way to grab this key?

var app = angular.module("sampleApp", ["firebase"]);
app.controller("SampleCtrl", function($scope, $firebaseArray, 

$firebaseAuth, $http) {
  var auth = $firebaseAuth();

  $scope.logIn = function login(){
    auth.$signInWithPopup("google").then(function(firebaseUser) {
      console.log("Signed in as:", firebaseUser.user.displayName);
    }).catch(function(error) {
      console.log("Authentication failed: ", error);
    });
  };

  auth.$onAuthStateChanged(function(firebaseUser){
    // firebaseUser will be null if not logged in
    if(firebaseUser) {
      // This is where we make our call to our server
      $http({
        method: 'GET',
        url: '/secretData',
        headers: {
          id_token: firebaseUser._lat
        }
      }).then(function(response){
        $scope.secretData = response.data;
      });
    }else{
      console.log('Not logged in.');
      $scope.secretData = "Log in to get some secret data."
    }

  });

  $scope.logOut = function(){
    auth.$signOut().then(function(){
      console.log('Logging the user out!');
    });
  };
});

完整代码可以在 github

推荐答案

不清楚您要做什么.不要访问混淆的内部属性,因为这些会发生变化.你已经注意到了.正确的方法是在onAuthStateChanged监听器中调用getToken:

It is not clear what you are trying to do. Do not access obfuscated internal properties as these will change. You already noticed that. The correct way is to call getToken in the onAuthStateChanged listener:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    user.getIdToken().then(function(idToken) {
      console.log(idToken);
    });
  }
});

这篇关于在 $onAuthStateChanged 上找到正确的 Firebase Auth id_token的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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