Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise After first login redirect

Normally after_sign_up_path would work but now that i have confirmations, this goes to the trash.

I'm searching for a way to redirect a user on his FIRST SIGN IN, meaning that

  • sign_in_count == 0

  • last_sign_in == nil

so i added to my applications_controller.rb

def after_sign_in_path_for(user)
  if current_user.sign_in_count == 0
    welcome_path
  end
end

but of course this didn't work. What am i missing ?

like image 958
Mini John Avatar asked Nov 29 '22 11:11

Mini John


1 Answers

After testing, we found Devise sets the value of sign_in_count immediately after login, meaning that it's never going to be 0, it's going to be 1 for a first-time login:

#config/routes.rb
devise_for :users, controllers: { sessions: "sessions" }

#app/controllers/sessions_controller.rb
class SessionController < Devise::DeviseController

    def after_sign_in_path_for(resource)
        if resource.sign_in_count == 1
           welcome_path
        else
           root_path
        end
    end

end
like image 171
Richard Peck Avatar answered Dec 06 '22 23:12

Richard Peck