简体   繁体   English

即使用户已登录,Auth。$ getUser也会返回null(AngularFire,Firebase)

[英]Auth.$getUser returns null even though user is logged in (AngularFire, Firebase)

Building a simple Ionic/Firebase card feed app, problem with getting current user from Firebase after they've already logged in. App structure is a basic login state that transitions to an abstract tabs state that contains two child tabs -- a card feed tab and an account tab. 构建一个简单的Ionic / Firebase卡片供稿应用程序,在他们已经登录后从Firebase获取当前用户的问题。应用程序结构是一个基本的登录状态,转换为包含两个子标签的抽象标签状态 - 卡片供稿标签和帐户标签。

Problem 问题

Need to get the current user data after they login to display their account info. 登录后需要获取当前用户数据以显示其帐户信息。 Firebase has a $firebaseAuth service with a $getAuth method that keeps returning null even though the user is logged in. Seems like user can log in but somehow isn't getting authenticated -- how can that be? Firebase有一个带有$ getAuth方法的$ firebaseAuth服务,即使用户已登录也会一直返回null。看起来像用户可以登录但不知何故未经过身份验证 - 这怎么可能? From Firebase docs: 来自Firebase文档:

$getAuth() $ getAuth()

Synchronously retrieves the current authentication state of the client. 同步检索客户端的当前身份验证状态。 If the user is authenticated, an object containing the fields uid (the unique user ID), provider (string identifying the provider), auth (the authentication token payload), and expires (expiration time in seconds since the Unix epoch) - and more, depending upon the provider used to authenticate - will be returned. 如果用户经过身份验证,则包含字段uid(唯一用户ID),提供程序(标识提供程序的字符串),auth(身份验证令牌有效负载)和到期(自Unix时代以来的到期时间,以秒为单位)的对象 - 以及更多,取决于用于验证的提供商 - 将被退回。 Otherwise, the return value will be null. 否则,返回值将为null。

Details 细节

Inside app.js I set up a Factory named 'Auth' inside that makes the connection to Firebase: app.js内部我设置了一个名为'Auth'的工厂,它与Firebase建立连接:

.factory('Auth', function($firebaseAuth) {
  var usersRef = new Firebase("https//myApp.firebaseio.com");
  return $firebaseAuth(usersRef);
})

Auth is injected into UserService which makes the $getAuth call and returns it to Account Tabs Controller. 将Auth注入UserService,进行$ getAuth调用并将其返回给Account Tabs Controller。 UserService is injected into Account Tab Controller which loads the account data (or should be). UserService被注入到帐户选项卡控制器中,该控制器加载帐户数据(或应该是)。

UserService UserService

//Uses Auth factory to access $firebaseAuth methods
angular.module('user.services')
    .service('UserService', ['$q', 'Auth', '$firebaseObject','$firebaseAuth',
        function($q, Auth, $firebaseObject, $firebaseAuth) {
            return {
                currentUser: function() {  //User is logged in at this point
                   return Auth.$getAuth(); //Returns null but should return current state
                },
             ... more functions
         }
 ]);

Accounts Tab Controller 帐户标签控制器

// User is logged in at this point
angular.module('app.controllers')
.controller('AccountPageController', [
    '$state', '$scope', 'UserService', '$firebaseAuth',  // <-- controller dependencies
    function($state, $scope, UserService, $firebaseAuth) {        
        $scope.user = UserService.currentUser();
        console.log("user = " + $scope.user); //Evaluates to null
        ... more functions
    }
]);

How can I get the current user? 我如何获得当前用户? Thank you in advance for your help, much appreciated :) 提前感谢您的帮助,非常感谢:)

Try binding the controller with the template in the router instead of the view. 尝试将controller与路由器中的template绑定,而不是视图。 ie

Instead of: 代替:

<div ng-controller="AccountPageController">
...
</div>

Try this in your .state() 在.state()中尝试这个

$stateProvider
  .state(
    ...
    templateUrl:'path/to/template.html',
    controller:'AccountPageController'
  );

Good Luck. 祝好运。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM