2013-03-21 3 views
5
에서 로그 아웃 할 궁리를 할 수 없습니다

나는 GET을 통해 로그 아웃 고안 인증을했다,하지만이 Angular.js 코드를 사용하여 로그 아웃 만들 수 없습니다 :이 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') 

고안의 로그 아웃 동작은 것을 무작위로 - 가끔 로그 아웃하거나 때로는 로그 아웃하지 마십시오. 그리고 브라우저 주소창에 /users/sign_out을 입력하면 항상 로그 아웃됩니다. 좋아

, 나는 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_outcurrent_user 항상 전무는 것을 발견,하지만 약간의 기적에 의해 각 응용 프로그램은 ApiController의 다른 방법에 액세스 할 수 관리 :

나는 내 자신의 방법이 어떻게되는지하기로 결정 current_user는 거기에 nil이 아닙니다!

나는 그것을 이해하지 못한다. 좋습니다, 다른 HTTP 요청을 따르거나, 로그 아웃 요청 직후에 (또는 동시에) 인증 쿠키와 Devise 재 로그인을 전달할 수도 있지만 은 쿠키에서 전달 된 세션 ID가 만료되지 않아야합니다. sign_out 메서드 호출 직후에?!

+0

당신은 sign_out [route a : devise.rb에서 경로를 가져 왔습니까?] (http://stackoverflow.com/questions/6557311/no-route-matches-users-sign-out-devise-rails-3)를 만들었습니까?) – shicholas

+1

@shicholas : 물론 – Paul

+0

레일 쿠키 또는 ng 쿠키는 어떻습니까? – shicholas

답변

2

가 미안 해요, 이전 응답하지 않습니다이

내 Sesisons 컨트롤러

당신이 볼 수 있듯이

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 

를 오버라이드

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

내 고안 세션 컨트롤러 도움이되기를 바랍니다 결코, 나는 아니에요 Rails 쿠키를 사용하므로 내 답변이 관련되지 않을 수 있습니다. 만약 내가했다면 아마도 세션 [: user] = nil과 같은 라인을 파괴 액션에 추가 할 것입니다.

+0

이제'$ cookieStore.remove' 호출을 제거하고 실제로 서버 측에서 로그 아웃하는지 확인하십시오. Btw : Devise는'_pf_session','_pf_name','_pf_email'이라는 이름의 쿠키가 없습니다 – Paul

+0

두 시스템은 병렬로 실행되며 JSON을 통해서만 서로 통신합니다. 로그인시 Angular는 브라우저에'pf_session','pf_name' 및'pf_email' 쿠키를 만듭니다. 서버는 생성 된 토큰이없는 요청이 승인되지 않았는지 확인합니다. 승인되지 않은 요청이 전송되면 [Witold의 HTTP 인터셉터] (http://witoldsz.github.io/angular-http-auth/)를 사용하여 권한이없는 요청이있을 때 클라이언트 측 앱을 해제합니다. 그러나 지난 밤에 내 루비 그룹과 이야기를 나눈 후 CSRF 문제가있을 수 있으며 레일 쿠키를 사용해야합니다. 나는 그것을 알아낼 때 게시 할 것입니다. – shicholas