devise icon indicating copy to clipboard operation
devise copied to clipboard

No route matches [GET] "/users/sign_out"

Open alpha-qu opened this issue 9 years ago • 17 comments

This is what I get on inspecting 'Log Out' <a rel="nofollow" data-method="delete" href="/users/sign_out">Log out</a>

This is what I get after clicking 'Log Out' No route matches [GET] "/users/sign_out"

This is my User.rb ` class User < ActiveRecord::Base

devise :database_authenticatable, :trackable, :validatable
devise :omniauthable, :omniauth_providers => [:google_oauth2]
def self.from_omniauth(access_token)
data = access_token.info
user = User.where(:email => data["email"]).first

unless user
    user = User.create(
       email: data["email"],
       password: Devise.friendly_token[0,20]
    )
end
user
end

end`

This is the output of rake routes Prefix Verb URI Pattern Controller#Action unauthorised GET /unauthorised(.:format) dashing/extras#unauthorised access GET /access(.:format) dashing/extras#access root GET / dashing/extras#home GET /:name(.:format) dashing/extras#show 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_google_oauth2_omniauth_authorize GET|POST /users/auth/google_oauth2(.:format) users/omniauth_callbacks#passthru user_google_oauth2_omniauth_callback GET|POST /users/auth/google_oauth2/callback(.:format) users/omniauth_callbacks#google_oauth2 dashing /dashing Dashing::Engine

Routes for Dashing::Engine: events GET /events(.:format) dashing/events#index GET /dashboards/:name(.:format) dashing/dashboards#show dashboards GET /dashboards(.:format) dashing/dashboards#index GET /widgets/:name(.:format) dashing/widgets#show PUT /widgets/:name(.:format) dashing/widgets#update root GET / dashing/dashboards#index

This is what my homepage says: <h1> HOME </h1> <%= link_to 'Sign In with Google', omniauth_authorize_path(:user, :google_oauth2) %> <%= link_to "Log out", destroy_session_path(:user), method: :delete %>

alpha-qu avatar Jun 24 '17 16:06 alpha-qu

Please use the mailing list or StackOverflow for questions/help, where a wider community will be able to help you. We reserve the issues tracker for issues only.

rafaelfranca avatar Jun 26 '17 22:06 rafaelfranca

Facing the same issue, searched on stackoverflow. Over there people are telling to add external input.

For example:

devise_for :users do
  get '/users/sign_out' => 'devise/sessions#destroy'
end

mittalyashu avatar Sep 09 '17 15:09 mittalyashu

I solved this issue using jquery-ujs. If you look into the readme it says:

make non-GET requests from hyperlinks

jorgearimitsu avatar Aug 07 '18 02:08 jorgearimitsu

@jorgearimitsu Thanks for the reminder on that one - I'd switched to webpack and hadn't imported jquery-ujs in my application pack, though I had remembered to add it to my yarn package:

import 'jquery-ujs';

Easy fix but could've been a headache if not for your comment. : )

bobf avatar Oct 14 '18 07:10 bobf

config.sign_out_via = :get

ibrahimabdelkhaleq avatar Jan 31 '19 13:01 ibrahimabdelkhaleq

For gem 'jquery-rails', '~> 4.3', '>= 4.3.5'

//= require jquery_ujs

kirykr avatar Aug 01 '19 16:08 kirykr

config/initializers/devise.rb ->

# The default HTTP method used to sign out a resource.  The Default is :delete.
# Changing to :get request solved my problem. Also, do remember to restart
# your server, else the changes will not be taken by the application.

config.sign_out_via = :get

SantoshVKamat avatar Aug 13 '20 14:08 SantoshVKamat

If you want to use :delete method for security reasons and not be dependent on jquery-ujs you can use button_to instead of link_to, like: button_to "Log out", destroy_user_session_path, method: :delete

pjbelo avatar Dec 08 '20 18:12 pjbelo

If you want to use :delete method for security reasons and not be dependent on jquery-ujs you can use button_to instead of link_to, like: button_to "Log out", destroy_user_session_path, method: :delete

This is super useful since if you want to use https://github.com/hotwired/turbo-rails you may have removed ujs.

oliviermilla avatar Dec 29 '20 23:12 oliviermilla

Please use the mailing list or StackOverflow for questions/help, where a wider community will be able to help you. We reserve the issues tracker for issues only.

@rafaelfranca How is the not an issue? He's using the API as specified, and it's not working like it's supposed to. Are you sure this isn't a bug in Rails?

Andrew-CC-Martin avatar Feb 17 '21 04:02 Andrew-CC-Martin

@Andrew-CC-Martin the issue doesn't seem to be Devise or Rails, it appears he's set up a link with data-method=delete but is getting a GET request on the server, which most likely indicates that UJS is not present to handle the data-method for links. Which means it may be more of a config issue, so help required, not necessarily a bug/issue.

All of the suggestions above may apply to get it working:

  • use button_to instead of link_to with data-method
  • if using links, use rails-ujs so that it can apply different data-methods.
  • change config.sign_out_via = :get as a last resort, if you're okay with logout via GET.

carlosantoniodasilva avatar Feb 17 '21 11:02 carlosantoniodasilva

Are there any reasons why you would NOT be ok with signout via GET?

paulodeon avatar Aug 20 '21 12:08 paulodeon

Are there any reasons why you would NOT be ok with signout via GET?

If I remember correctly, GET is expected to be a safe transaction that won't tell the server to do anything. POST on the other hand expect things to happen. Example; If your browser is helpfully speeding up your web interactions by preloading things you've not yet decided to read (and follows every link on a page to preload it), it will follow a GET request and in this case would unexpectedly log you out.

steveroot avatar Dec 21 '21 16:12 steveroot

If you want to use :delete method for security reasons and not be dependent on jquery-ujs you can use button_to instead of link_to, like: button_to "Log out", destroy_user_session_path, method: :delete

Thank you so much, this solved my problem

emmanuelkamala avatar Jan 25 '22 23:01 emmanuelkamala

If you want to use :delete method for security reasons and not be dependent on jquery-ujs you can use button_to instead of link_to, like: button_to "Log out", destroy_user_session_path, method: :delete

in result, this is the safest way, thank you

i4pg avatar Mar 06 '22 05:03 i4pg

Use this with turbo link_to 'LogOut', destroy_user_session_path, data: { turbo_method: :delete }

Pako7 avatar Aug 15 '24 22:08 Pako7

config/initializers/devise.rb ->

# The default HTTP method used to sign out a resource.  The Default is :delete.
# Changing to :get request solved my problem. Also, do remember to restart
# your server, else the changes will not be taken by the application.

config.sign_out_via = :get

thanks it worked for me just don t forget to restart the server

nightmere33 avatar Sep 20 '24 23:09 nightmere33