-
Notifications
You must be signed in to change notification settings - Fork 5.5k
How To: Change the default sign_in and sign_out routes
When you are using only one role with Devise you may want to change the sign in and sign out routes to /login and /logout (instead of /users/sign_in and /users/sign_out).
Luckily, Devise provides a mechanism to specify a default scope, allowing us to have short URLs.
All you need to do is to specify in your routes the devise_scope being accessed in that URL:
devise_scope :user do
get 'login', to: 'devise/sessions#new'
endSince devise_scope is aliased to as, this is equivalent:
as :user do
get 'login', to: 'devise/sessions#new'
endSimilarly for sign_out:
devise_scope :user do
delete 'logout', to: 'devise/sessions#destroy'
endNote that you can skip all sessions routes and define only your own using the skip option as below:
devise_for :users, skip: [:sessions]
as :user do
get 'signin', to: 'devise/sessions#new', as: :new_user_session
post 'signin', to: 'devise/sessions#create', as: :user_session
delete 'signout', to: 'devise/sessions#destroy', as: :destroy_user_session
endThis way :authenticate_user! and other helpers will be redirecting the user to the proper custom pages you defined.
Note that if you are making use of the :sign_out_via configuration option, then the signout action above may cause errors. You can duplicate the default behavior (which changes from delete to get based on :sign_out_via) by specifying:
devise_for :users, skip: [:sessions]
as :user do
get 'signin', to: 'devise/sessions#new', as: :new_user_session
post 'signin', to: 'devise/sessions#create', as: :user_session
match 'signout', to: 'devise/sessions#destroy', as: :destroy_user_session, via: Devise.mappings[:user].sign_out_via
endThe devise_for method has a lot of optional parameters to make things easier. If you just want to remove the users namespace before all the routes and rename sign_in, sign_out to login, logout. Try this way:
devise_for :users, path: '', path_names: { sign_in: 'login', sign_out: 'logout'}If you want to use multiple Devise controllers (like passwords) you should still use the first way.