【问题标题】:AngularJS + Rails + Devise - Logout issueAngularJS + Rails + Devise - 注销问题
【发布时间】:2014-02-02 01:05:32
【问题描述】:

我正在制作一个 AngularJS 应用程序,使用 Rails 作为 API 和 Devise gem 进行身份验证。

目前我正在使用我的 rails home 控制器:

def index
  if user_signed_in?
    set_csrf_cookie_for_angular
    render Rails.root.join('public', 'angular_app').to_s, layout: false
  else
    redirect_to new_user_session_path
  end
end

基本上,如果用户已登录,则转到实际上是重命名的 index.html 文件的“angular_app”,否则转到设计登录表单。

这很好用,我可以做到

before_filter :authenticate_user!

对于所有需要登录的页面,我可以处理来自 Angular 拦截器的 401。

现在我遇到的问题是将用户从 Angular 重定向到登录页面

.config(function ($httpProvider){
        $httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content');
        var interceptor = ['$location', '$rootScope', '$q', function($location, $rootScope, $q) {
            function success(response) {
              return response;
            };

            function error(response) {
                if (response.status == 401) {
                    $rootScope.$broadcast('event:unauthorized');
                    $location.path('/users/sign_in');
                };
            };

            return function(promise) {
                return promise.then(success, error);
            };
        }];
        $httpProvider.responseInterceptors.push(interceptor);
  })

location.path 将 URL 更改为正确的路径 - 但在我手动刷新页面之前不会显示表单。刷新时通过控制台查看它会执行 GET 然后加载页面。有没有办法可以在角度上模仿这个?

如果有人有更好的处理方法,请告诉我!

【问题讨论】:

    标签: ruby-on-rails angularjs devise


    【解决方案1】:

    您可以使用$window.location.href = '/users/sign_in' 强制刷新浏览器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-18
      • 2016-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-11
      • 2020-08-22
      相关资源
      最近更新 更多