Unable to get Devise to exit Angular.js

I did Devise authentication to log out via GET, but could not log out using this Angular.js code:

$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')

Identifying the behavior of logging out seems random - sometimes it logs out, sometimes not. And if I type /users/sign_outin the address bar of the browser, it will always log out.

Ok, I switched Devise authentication authentication to a POST request to get rid of caching issues and used the following Angular.js code:

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

The first time he logged out, as always, but then I could not log out of it.

I decided to make my own method to find out what happens:

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 nil, Angular - ApiController, current_user !

. , , HTTP ( ) , cookie , cookie sign_out?!

+5
1

, , ,

Sesisons

$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);
  });
} 

My Devise Sessions Controller overrode

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, . , , ​​ session [: user] = nil destroy.

+2

All Articles