0

Problem

With Devise the user should be redirected to the edit_user_registration_path of the Devise controller registrations#edit action when already logged in.

Description

In the routes.rb the root is set as root :to => 'main#welcome'. The User is allways redirected to this page, even when already logged in.

Thanks a lot Fabrizio

Fabrizio Bertoglio
  • 5,890
  • 4
  • 16
  • 57
  • 1
    if you have not seen this yet, it may help you out, https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview – Rockwell Rice Feb 16 '17 at 15:44
  • @RockwellRice Thanks, yes I read it. I am searching online for bugs, It is a lost session so I am testing the following solutions http://stackoverflow.com/questions/5816294/omniauthfacebook-lost-session – Fabrizio Bertoglio Feb 16 '17 at 15:48
  • @RockwellRice I am thinking of accepting your answer. For this specific issue I will create another question. What do you think? – Fabrizio Bertoglio Feb 16 '17 at 16:13

3 Answers3

1

Devise provides the user_signed_in? method.

This would be the welcome method in you main_controller.rb

def welcome 

    if user_signed_in?
        redirect_to edit_user_registration_path
    else
        // put other page in here
    end        
end
Rockwell Rice
  • 3,376
  • 5
  • 33
  • 61
  • I think your answer is the correct one. My user_signed_in? is always false, even after sign in. I tested and read the value both with facebook omniauth and normal sign_in/sign_up . Thanks – Fabrizio Bertoglio Feb 16 '17 at 14:44
  • 1
    you could try "if current_user" instead, not really a fix but could at least I get it going in the right direction and help debug. If that is still false even after sign in then something is wrong with the implementation and you should look at the database to see what records are being set. – Rockwell Rice Feb 16 '17 at 14:57
  • Thanks, I need to correct myself. The `user_signed_in?` method does **not work** when I log in with **facebook omniauth**, while It works with normal users. I tried with `current_user`. When I log in with facebook omniauth, it will redirect me to my profile. If I visit then localhost:3000 with send me to main#welcome and triggers `if current_user` with `current_user = nil` – Fabrizio Bertoglio Feb 16 '17 at 15:04
  • 1
    I would update the original post to mention omniauth. – Rockwell Rice Feb 16 '17 at 15:13
1

Maybe what you are looking for is a way to redirect the user to some place after he logs in. This should do it. Put this in app/controllers/application_controller.rb and place in the path you want to take the user to

  def after_sign_in_path_for(resource)
    edit_user_registration_path # or any other path needed
  end
Alok Swain
  • 6,409
  • 5
  • 36
  • 57
  • 1
    Thanks a lot. Actually I need to redirect the user independently from the Sign In. I already edited that `after_sign_in_path_for(resource)` and It worked, but right now I am trying to redirect him when he is already signed in. – Fabrizio Bertoglio Feb 16 '17 at 14:46
0

It could be misleading, but the user is signed in by devise after sign up method called inside Devise::RegistrationsController#create

class Devise::RegistrationsController < DeviseController

  # POST /resource
  def create
    build_resource(sign_up_params)
    # omitted code
    resource.save
    sign_up(resource_name, resource)

which sign_in() the user

def sign_up(resource_name, resource)
  sign_in(resource_name, resource)
end

so the method to be enhanced is

def after_sign_in_path_for(resource)
  edit_user_registration_path
end
Fabrizio Bertoglio
  • 5,890
  • 4
  • 16
  • 57