在Angular和Firebase中全面展示当前登录的用户 [英] Expose current logged in user globally in Angular and Firebase

查看:159
本文介绍了在Angular和Firebase中全面展示当前登录的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种清晰优雅的方式,使当前登录的用户暴露在我的应用程序中的任何地方,因为我有一个导航局部视图,其中包含用户信息以及注销按钮,但我还没有要做到这一点。

我的出发点是这个示例应用程序 https://github.com/firebase/angularfire-seed
我刚刚添加一个AppCtrl,我可以公开当前登录的用户。我已经尝试添加用户提供程序,但也不起作用。



controllers.js

  .controller('AppCtrl',['$ scope','$ rootScope','simpleLogin','fbutil','$ location' $ b $ scope.user = simpleLogin.getUser(); $ b $ scope.logout = function(){
var profile = fbutil.syncObject() ['users',simpleLogin.getUser()。uid]);
profile。$ destroy();
simpleLogin.logout();
$ location.path('/ login') ;
};
}])

index.html

 < body ng-controller =AppCtrl> 

您的帮助将会非常值得您的赞赏。

解析参数 - 它存在于 $ routerProvider $ stateProvide resolve 解析对象(包括解析承诺),然后这些对象可用于您的控制器。在这种情况下,您可以解析您的loggedInUser变量(通过您通过身份验证服务执行任何您需要的操作)。
$ b $ pre $ $ $ $ $ $
。当( / someSecuredContent,{
templateUrl: 'someSecuredContent.html',
控制器: 'SecuredController',
决心:{
与loggedInUser:功能( MyAuth){
return MyAuth.loggedIn(); // MyAuth.loggedIn()should return a $ q promise
}
}
});

然后在控制器中注入 loggedInUser



以下是 site 更多的例子。


I'm looking for a clear and elegant way to have the current logged in user exposed everywhere in my app because I have a navigation partial view that contains the user information as well as a logout button but I haven't been able to accomplish that.

My starting point is this sample app https://github.com/firebase/angularfire-seed and I just added an AppCtrl where I can expose the current logged in user. I have tried adding the user provider but that doesn't work either.

controllers.js

.controller('AppCtrl', ['$scope', '$rootScope','simpleLogin', 'fbutil', '$location', function($scope, $rootScope, simpleLogin, fbutil, $location) {
  $scope.user = simpleLogin.getUser();
  $scope.logout = function() {
    var profile = fbutil.syncObject(['users', simpleLogin.getUser().uid]);
    profile.$destroy();
    simpleLogin.logout();
    $location.path('/login');
  };
}])

index.html

<body ng-controller="AppCtrl">

Your help will be very appreciated.

解决方案

Take a look at the resolve parameter - this exists both in $routerProvider and $stateProvide. resolve resolves the objects (including resolving a promise) and then these objects are available to your controllers. In this case, you "resolve" your loggedInUser variable (by doing whatever you need to do via your authentication service.

$routeProvider
   .when("/someSecuredContent", {
     templateUrl: 'someSecuredContent.html',
     controller: 'SecuredController',
     resolve: {
        loggedInUser: function(MyAuth){
           return MyAuth.loggedIn(); // MyAuth.loggedIn() should return a $q promise
        }
     }
});

Then in the controller, loggedInUser will be injected.

Here's a site with more examples.

这篇关于在Angular和Firebase中全面展示当前登录的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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