0
votes

I'm trying to create a rails 5 applications with devise that's supposed to act like a social network, so every user has a profile. After updating the user instead of going back to the profile, the server redirects to localhost, then there is a message Filter chain halted as :require_no_authentication rendered or redirected and only then the user profile is loaded. Also on the profile a message is shown "You are already signed in". My goal is to erase those redirects.

  1. I tried to change the redirect in a custom RegistrationController but then it said there are too many redirects so I could't just replace it.
  2. I tried to have different roots for authenticated and unauthenticated users in routes.rb but it didn't change the behaviour
  3. I tried to get behind the :require no authentication but I'm actually not quite sure I understood it correctly and also it didn't change anything.
  4. The after_sign_in_path_for is set to the user profile

This is the server output:

Started PUT "/users" for 127.0.0.1 at 2019-06-04 12:13:37 +0200
Processing by Users::RegistrationsController#update as HTML
  Parameters: {"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmat
ion"=>"[FILTERED]", "current_password"=>"[FILTERED]"}, "commit"=>"Update"}
User Load (0.8ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
User Load (0.6ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT
 1
   (0.3ms)  BEGIN
   (0.4ms)  COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 295ms (ActiveRecord: 2.1ms)


Started GET "/" for 127.0.0.1 at 2019-06-04 12:13:37 +0200
Processing by Users::SessionsController#new as HTML
  User Load (0.7ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER
 BY `users`.`id` ASC LIMIT 1
Redirected to http://localhost:3000/users/1
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 15ms (ActiveRecord: 0.7ms)


Started GET "/users/1" for 127.0.0.1 at 2019-06-04 12:13:37 +0200
Processing by UsersController#show as HTML
  Parameters: {"id"=>"1"}
  User Load (0.6ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER
 BY `users`.`id` ASC LIMIT 1
  User Load (0.5ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT
 1
  Rendering users/show.html.erb within layouts/application
  Rendered users/show.html.erb within layouts/application (1.1ms)
  Rendered shared/_navbar.html.erb (0.7ms)
Completed 200 OK in 162ms (Views: 145.3ms | ActiveRecord: 1.1ms)

Also why does the user get signed in again after updating? Is it correct behaviour? I cannot seem to fathom where the redirect to localhost ist happening and intervene. Any help would be appreciated.

Edit 1

So, to clarify. I tried to use an authenticated_root but then I get the errormessage Couldn't find User without an ID.

devise_scope :user do

authenticated :user do
    root to: 'users#show', as: :authenticated_root
  end
  root to: 'users/sessions#new'
end

Also I can't manage to change the redirecting from to root to the user profile. So the RegistrationController is unchanged. I would like to say in the update method or somewhere else "after updating go to user profile".

3
So you're redirecting to the root path after the user is updated? Did you make sure to put your root routes at the bottom of your routes file? Can you post the relevant routes as well as any controller overrides that you may have created?NM Pennypacker

3 Answers

0
votes

It seems like your root_path is /users/sign_in, and when the user updates his account, you redirect him to the root... but the user is already signed in so he can't access to the new_user_session_pah. The fallback default location configured in Devise is the user's profile, so he's redirected to this route.

Have you configured an authenticated root? https://github.com/plataformatec/devise/wiki/How-To:-Define-a-different-root-route-for-logged-in-out-users

It should fix your problem (:

0
votes

I managed to solve it by adding a method to RegistrationController as follows:

def after_update_path_for(resource)
  user_path(current_user)
end
0
votes

That is, after updating user, Rails goes to root_path, that is sessions#new. But user's already logged in, so rails router redirects you to users#show

Why don't you set root "home#index" (or sth like that)

Rails.application.routes.draw do
  devise_for :users

  authenticated :user do
    root 'secret#index', as: :authenticated_root
  end

  root "home#index"
end

and then, in your application_controller.rb you can just require users to login before using your page

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :authenticate_user!
end

If you want to redirect user to users#show after update, you can override the after_update_path_for

class User::RegistrationsController < Devise::RegistrationsController

  protected

  def after_update_path_for(resource)
    user_path(resource)
  end
end