Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to devise destroy session and sign out from controller?

Devise destroy session and sign out from controller?

if something_is_not_kosher
  # 1. log this event, 2. send notice
  redirect_to destroy_user_session_path and return
end

Also tried:

if something_is_not_kosher
 # 1. log this event, 2. send notice
  redirect_to controller: 'devise/sessions', action: 'destroy', method: :delete and return
end

Error is No route matches [GET] "/users/sign_out" but I'm explicitly setting method: :delete in example 2. Maybe devise has a method? current_user.sign_out and tried sign_out(current_user) which also don't work? Thanks for the help.

rake routes:

        new_user_session GET    /users/sign_in(.:format)         devise/sessions#new
            user_session POST   /users/sign_in(.:format)         devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)        devise/sessions#destroy
           user_password POST   /users/password(.:format)        devise/passwords#create
       new_user_password GET    /users/password/new(.:format)    devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)   devise/passwords#edit
                         PATCH  /users/password(.:format)        devise/passwords#update
                         PUT    /users/password(.:format)        devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)          users/registrations#cancel
       user_registration POST   /users(.:format)                 users/registrations#create
   new_user_registration GET    /users/sign_up(.:format)         users/registrations#new
  edit_user_registration GET    /users/edit(.:format)            users/registrations#edit
                         PATCH  /users(.:format)                 users/registrations#update
                         PUT    /users(.:format)                 users/registrations#update
                         DELETE /users(.:format)                 users/registrations#destroy
like image 581
MrPizzaFace Avatar asked Sep 15 '14 23:09

MrPizzaFace


3 Answers

Why don't you just use devise's built-in sign_out_and_redirect(current_user) method?

like image 69
AOG Avatar answered Nov 04 '22 20:11

AOG


So I ended up solving this by creating a custom signout route

  devise_scope :user do
    get '/signout', to: 'devise/sessions#destroy', as: :signout
  end

and in my controller I have:

if something_is_not_kosher
  redirect_to signout_path and return
end
like image 37
MrPizzaFace Avatar answered Nov 04 '22 19:11

MrPizzaFace


destroy_user_session_path(@user) is sign out path for user, but it must requst with DELETE method. redirect_to method will tell broswer to request another path, but broswer just can request with GET method.
So if you want to let user to sign out, you must set a sign out form with DELETE method or with AJAX request to let user sign out but not with redirect_to function.

If you just want to destroy user session, use sign_out @user is ok.

like image 6
dddd1919 Avatar answered Nov 04 '22 19:11

dddd1919