Devise - Changing the name of a routing - ruby-on-rails

By default Devise creates a routing for a sign-in as '/sign_in'.
How would I change the path so that it is '/log_in'?

By adding scope
devise_for :users
devise_scope :user do
get '/login' => 'devise/sessions#new'
get '/logout' => 'devise/sessions#destroy'
end

This is how I did it in the end, given that I was already inserting controllers to handle aspects of Devise:
in routes.rb
#Add Devise authentication to users, handling omniauth callbacks in users/omniauth_callbacks_controller
devise_for :users, :skip => [:sessions],
:controllers => { :omniauth_callbacks => 'users/omniauth_callbacks',
:registrations => 'users/registrations'
}
# :skip => [:sessions] tells devise not to create routes for sessions, allowing us to declare our own
as :user do
get 'users/log_in' => 'devise/sessions#new', :as => :new_user_session
post 'users/log_in' => 'devise/sessions#create', :as => :user_session
delete 'users/log_out' => 'devise/sessions#destroy', :as => :destroy_user_session
end

Related

Devise : overriding the devise routes and controllers at the same time

i want to ovverride my devise routes and the sessions controller from this gem at the same time. How do i do this ?
I thought about :
devise_for :admins, :skip => [:sessions],
controllers: { sessions: "admins/sessions" }
devise_scope :admin do
get 'login' => 'devise/sessions#new', :as => :new_admin_session
post 'login' => 'devise/sessions#create', :as => :admin_session
delete 'logout' => 'devise/sessions#destroy', :as => :destroy_admin_session
end
but my paths are chaging but the controller - not. How can I implement this right ?
When you specify controllers: { sessions: "admins/sessions" }, that implies that you have a file called sessions_controller.rb in this path: app/controllers/admins/sessions_controller.rb, and that it starts with:
module Admins
class SessionsController < Devise::SessionsController
If this is the controller you want your app to use, then in the devise_scope block, you must tell it to use admins/sessions, not devise/sessions, like this:
devise_scope :admin do
get 'login' => 'admins/sessions#new', :as => :new_admin_session
post 'login' => 'admins/sessions#create', :as => :admin_session
delete 'logout' => 'admins/sessions#destroy', :as => :destroy_admin_session
end
What about something like:
devise_for :admin, exclude: [:sessions] do
get '/login', to: 'sessions#new', as: :new_admin_session
post '/login', to: 'sessions#create', as: :admin_session
delete '/logout', to: 'sessions#destroy', as: :destroy_admin_session
end

Could not find devise mapping for path "/". before the user has signed in

I am getting the following error when i go to root after logging in
Could not find devise mapping for path "/".
This may happen for two reasons:
1) You forgot to wrap your route inside the scope block. For example: devise_scope :user do get "/some/route" => "some_devise_controller" end
2) You are testing a Devise controller bypassing the router. If so, you can explicitly tell Devise which mapping to use: #request.env["devise.mapping"] = Devise.mappings[:user]
Routes File
Rails.application.routes.draw do
root 'pages#home'
devise_for :users, controllers: { sessions: "users/sessions", registrations: "users/registrations", confirmations: "users/confirmations", passwords: "users/passwords" }, :skip => [:sessions]
as :user do
get 'sign_in' => 'users/sessions#new', :as => :new_user_session
post 'sign_in' => 'users/sessions#create', :as => :user_session
match 'sign_out' => 'users/sessions#destroy', :as => :destroy_user_session,
:via => Devise.mappings[:user].sign_out_via
end
end
Even though i have a route_path it throws up the error.
Try writing routes in scope block.
devise_scope :user do
# write all your routes inside this block
end
You can find more about scope here
https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes

Ruby on Rails & Devise, change 'users/sign_in' to 'sign-in' using omniauth_callbacks and registrations

I am trying to change the 'users/sign_in' to 'sign-in' route
This is my current route setup
devise_for :users, :controllers => { :registrations => 'registrations', :omniauth_callbacks => "authentications" }
devise_scope :user do
put 'update_plan', :to => 'registrations#update_plan'
put 'update_card', :to => 'registrations#update_card'
put 'charge', :to => 'registrations#charge'
get "/sign-up" => "users/registrations#new", :as => :new_user_registration
get '/sign-in' => "devise/sessions#new", :as => :new_user_session
post '/sign-in' => 'devise/sessions#create', :as => :user_session
get '/sign-out' => 'devise/sessions#destroy', :as => :destroy_user_session
end
but these routes give me the error ArgumentError: Invalid route name, already in use: 'new_user_registration'
can I move the :controllers => { :registrations => 'registrations', :omniauth_callbacks => "authentications" } bit into devise_scope?
If i remove :as => :new_user_registration from my routes then i get a redirect loop. I really can't figure this out
Any help is much appreciated thanks.
You need to tell Devise to skip session ULRs.
devise_for :users :controllers => { :registrations => 'registrations', :omniauth_callbacks => "authentications" }, :skip => [:sessions]
devise_scope :user do
put 'update_plan', :to => 'registrations#update_plan'
put 'update_card', :to => 'registrations#update_card'
put 'charge', :to => 'registrations#charge'
get "/sign-up" => "users/registrations#new", :as => :new_user_registration
get '/sign-in' => "devise/sessions#new", :as => :new_user_session
post '/sign-in' => 'devise/sessions#create', :as => :user_session
get '/sign-out' => 'devise/sessions#destroy', :as => :destroy_user_session
end
See How To: Change the default sign_in and sign_out routes

how to use new_user_session route from devise as the root path?

Problem - I don't know how to set the new_user_session route from devise gem as the root path in Rails app.
Rails.application.routes.draw do
devise_for :users
resources :dashboard
root to: "home#index"
Place this line in routes.rb
devise_scope :user do
root :to => 'devise/sessions#new'
end
You need to set default session route.
Replace with .
devise_for :users, :controllers => {:registrations => "registrations", :sessions => "sessions"}
devise_for :users do
get '/users/sign_in', :to => 'devise/sessions#new', :as => :new_user_session
get '/users/sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
end
and root to
devise_scope :user do
get "/" => "sessions#new"
end

Devise custom routes slippery slope

I started using custom routes w/Devise so that I could have my 'Sign In' and 'Sign Up' routes go to the same page. However, as soon as I followed the instruction from Devise about custom routes, it seems that every route now has to be explicitly specified. This has now broken my reset password links since that portion is handled by Devise.
What am I doing wrong here? You can see below that I've had to spell out everything for my User and UserSessions model. Shouldn't I only have to specify the ones I want to change?
devise_for :users, :controllers => { :sessions => "user_sessions" ,:registrations=>"users"},:skip => [:sessions] do
get 'users/sign_in' => 'user_sessions#new', :as => :new_user_session
get 'users/sign_up' => 'user_sessions#new', :as => :new_user_session
post 'users/sign_in' => 'user_sessions#create', :as => :user_session
post 'user_sessions' => 'user_sessions#create', :as => :app_sign_in
delete 'users/sign_out' => 'user_sessions#destroy', :as => :destroy_user_session
get 'users/sign_out' => 'user_sessions#destroy', :as => :destroy_user_session
post 'users/:id' => 'users#update', :as =>:update_user
get 'users' => 'users#index'
get 'users/:id/edit' => 'users#edit', :as => :edit_user
get 'users/:id' => 'users#show', :as => :show_user
delete 'users/:id' => 'users#destroy', :as => :destroy_user
end
Can you just use, not sure if this will work for you
devise_for :users
devise_for :users do
get '/users/sign_out' => 'devise/sessions#destroy'
end

Resources