yamaoka
yamaoka

Reputation: 131

How do you customize the route after a user logs-in or signs-in in Devise?

Currently, a user gets redirected to a root index page after sign-in/log-in but I want to customize it so that the redirect goes to a different page.

My route file is:

Rails.application.routes.draw do
  devise_for :admins, path: 'admins'
  root 'home#index'
  get '/' => "courses#index", as: :user_root
  devise_for :users, path: 'users'
    resources :courses, :lessons
end

I understand that by default, the root become where the redirect goes to so I used the code get '/' => "courses#index", as: :user_rootand the redirect worked as I wanted to. However, when a user logs-out, the redirect tries to go to get '/' => "courses#index", as: :user_rootonce again which I do no want. Instead, I want the redirect when a user logs-out to go to root 'home#index'.

So essentially my question is how can I customize my root so I can achieve different redirects depending on whether a user logs-in/signs-in and logs-out?

Upvotes: 0

Views: 62

Answers (1)

yellowmint
yellowmint

Reputation: 130

You can use this:

class ApplicationController < ActionController::Base

private
  def after_sign_in_path_for(resource)
    user_root_path
  end

  def after_sign_out_path_for(resource_or_scope)
    root_path
  end
end

It is on devise wiki:

signing in

signing out

Upvotes: 2

Related Questions