如何在使用angular_devise注销之前保持全局当前用户? [英] How to keep globally current user until logout with angular_devise?

查看:21
本文介绍了如何在使用angular_devise注销之前保持全局当前用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建自己的可全局访问的服务,该服务将在页面加载时通过 currentUser() 调用服务器,如果用户已登录,则保留它并向控制器或状态提供数据直到注销?现在我在许多状态或控制器 currentUser() 中多次解析,我在文档中找到:authparseresponse 有可能制作自己的服务,但我不知道如何以正确的方式处理.

How to create own service accessible globally that will call server by currentUser() only once on page load, if User is logged in then keep it and provide data to controllers or states until logout? Now I'm resolving many times in many states or controllers currentUser(), I found in docs: authparseresponse that it is possibility to make own service, but I don't know how to handle this in right way.

例如在 ui-router 中,我有两个服务器调用,只有在加载页面时才足够:

e.g in ui-router I've two server calls, would be sufficient only when page is loaded:

.state('login', {
  url: '/login',
  templateUrl: 'auth/_login.html',
  controller: 'AuthCtrl',
  onEnter: [
    '$state', 'Auth',
    function($state, Auth) {
      Auth.currentUser().then(function() {
        $state.go('home');
      });
    }
  ]
})
.state('dashboard', {
  url: '/dashboard',
  templateUrl: 'dashboard.html',
  onEnter: [
    '$state', 'Auth',
    function($state, Auth) {
      Auth.currentUser().then(function(user) {}, function(err) {
        $state.go('home');// user not logged in
      });
    }
  ]
})

或每次在 NavController 中调用服务器:

or each time next server call in NavController:

app.controller('NavController', [
  '$state', '$scope', 'Auth', function($state, $scope, Auth) {
    $scope.signedIn = Auth.isAuthenticated;
    $scope.logout = Auth.logout; // logout function
    Auth.currentUser().then(function(user) {
      $scope.user = user;
    });
    $scope.$on('devise:login', function(e, user) {
      $scope.user = user;
    });
    $scope.$on('devise:logout', function(e, user) {
      $scope.user = {};
      $state.go("home");
    });
  }
]);

我发现了没有答案的类似问题:堆栈溢出

I found similar question without answer: stackoverflow

推荐答案

我们知道工厂是一个单例对象,基本上它用于数据共享.我们可以将其注入任何控制器并作为依赖项运行.所以这也可能解决你的问题.将此服务注入您的状态解析块并检查用户是否存在如果不存在则调用工厂的获取用户功能

As we know factory is a singleton object and basically it is used for data sharing. We can inject it to any controller and functions as an dependency .So this may be also solve your problem. inject this service to your state resolve block and check if user exists or not if not then call the fetch user function of factory

app.factory("User", function($http, $q, $rootScope) {
var user = null;
return {
    fetchUser: function() {
        /*your ajax call that return current user*/
        user = YOUR_SERVER_RESPONSE;
        return user;

    },
    getUser : function(){
        return user;
    },
    setUser : function(u){
        user = u;
    }
}

});

你的状态配置块看起来像

and your state config block is look like

  .state('dashboard', {
  url: '/dashboard',
  templateUrl: 'dashboard.html',
  resolve : function(User){
    if(User.getUser()){
        return User.getUser();
    }else{
        return User.fetchUser();
    }
  }
})

这篇关于如何在使用angular_devise注销之前保持全局当前用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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