不能让Devise从Angular.js注销

时间:2013-03-21 13:42:06

标签: ruby-on-rails devise angularjs logout

我使用Devise身份验证通过GET注销,但无法使用此Angular.js代码注销:

$scope.logout = ->
  $http.get('/users/sign_out').success ->
    #If it does not redirect from 'editor' to 'login' then you haven't actually logged out
    $location.path('editor')

Devise的退出行为似乎是随机的 - 有时会退出,有时则不会。 如果我在浏览器的地址栏中输入/users/sign_out,它会一直注销。

好的,我将Devise身份验证的注销切换为POST请求以摆脱缓存问题并使用以下Angular.js代码:

$scope.logout = ->
  $http.post('/users/sign_out').success ->
    $location.path('editor')

第一次它一如既往地退出正常,但我无法让它退出。

我决定用自己的方法来看看会发生什么:

match '/logout' => 'api#logout', :via => :post

class ApiController < ApplicationController
  before_filter :authenticate_user!

  def logout
    sign_out
    if current_user
      puts 'Has not signed out!'
    else
      puts 'Has signed out!'
    end
    head :ok
  end
end

并检测到sign_out之后current_user总是为零,但是一些奇迹的Angular应用程序设法访问ApiController的其他方法,而current_user不是零!

我不明白。好吧,让我们假设在注销请求之后(或同时)可能会遵循其他一些HTTP请求,传递身份验证cookie和设计重新登录,但不应该在cookie中传递会话ID在调用sign_out方法后立即过期?!

1 个答案:

答案 0 :(得分:2)

抱歉,我从未回复过,希望这有帮助

My Sesisons Controller

$scope.signOutUser = function () {
  $http.delete('/api/users/sign_out', {
    auth_token: Session.currentUser // just a cookie storing my token from devise token authentication.
  }).success( function(result) {
    $cookieStore.remove('_pf_session');
    $cookieStore.remove('_pf_name');
    $cookieStore.remove('_pf_email');
    location.reload(true); // I need to refresh the page to update cookies
  }).error( function(result) {
    console.log(result);
  });
} 

我的设计会话控制器我超越

class SessionsController < Devise::SessionsController
  before_filter :authenticate_user!, only: :destroy

  def destroy
    token = params[:auth_token]
    @user = User.find_by_authentication_token(token)
    @user.reset_authentication_token!
    sign_out(@user)
    render status: :ok, json: {message: "You have successfully logged out"}
  end
end

正如您所看到的,我没有使用Rails cookie,因此我的回答可能不适用。如果我这样做,我可能会在我的销毁行动中添加类似会话[:user] = nil的行。