clearance icon indicating copy to clipboard operation
clearance copied to clipboard

Clearance login redirect breaks after upgrading from Rails 6 beta1 to Rails 6 beta 2

Open smcalilly opened this issue 7 years ago • 3 comments

I'm using the Clearance gem for authentication on an app that I started new with Rails 6 beta 1. Everything was working fine, until I tried to upgrade to Rails 6 beta 2.

I've also tried the latest stable version of Clearance as well as Clearance 2.0.0.beta1 and I have the same problem.

I'm having trouble figuring out what might have broken my Clearance login.

I can verify in the console that the users are getting created by the Clearance Sign up form, so it just appears to be the login.

I did switch my routes from get '/login' => 'clearance/sessions#new', as: 'sign_in' to get '/login' => 'sessions#new', as: 'sign_in', but switching that back.

Any pointers on what change between Rails beta1 and beta2 that might be causing this error would be much appreciated!

smcalilly avatar Apr 28 '19 21:04 smcalilly

Can you attempt this with rails version 6.0.0.rc1 and report back? If possible, can you provide either a failing spec, or minimum code to reproduce?

I'm not sure exactly what you mean with your description of what you are doing with routes ... can you clarify this, and also confirm what the routes look like and that failure is isolated to JUST updating the rails version, and not also changing the routes while you do so?

mjankowski avatar Apr 29 '19 13:04 mjankowski

Thanks for responding. Hopefully this info helps.

We first upgraded to 6.0.0.rc1 and that's where we first encountered the problem. So we tried upgrading from 6.0.0.beta1 to 6.0.0.beta2 and still had the same problem.

Here are the relevant routes:

Rails.application.routes.draw do
  .
  .
  .
  # Accounts
  resources :accounts
  get '/dashboard' => 'accounts#show', as: 'dashboard'
  .
  .
  .
  # Users & Clearance
  resources :passwords, controller: 'passwords', only: [:create, :new]
  resource :session, controller: 'sessions', only: [:create]

  resources :users, controller: 'clearance/users', only: [:create] do
    resource :password,
      controller: 'clearance/passwords',
      only: [:create, :edit, :update]
  end
  get '/login' => 'sessions#new', as: 'sign_in'
  delete '/logout' => 'clearance/sessions#destroy', as: 'sign_out'
  get '/signup' => 'users#new', as: 'sign_up'
  get '/users' => 'clearance/users#index'
end

Upon signup, the user is supposed to be signed in and redirected to the account creation flow where they create an account by specifying their business name, then they land on their home page.

I can't successfully GET /signup and redirect to the next page on signup flow. Instead it redirects to the login page. Nor can I simply GET /login and redirect to the /dashboard page. It also redirect to the login page.

Here's a log snippet from the broken signup flow, after a successful POST to /users:

Redirected to http://localhost:3000/accounts/new
Completed 302 Found in 84ms (ActiveRecord: 4.7ms | Allocations: 5083)


Started GET "/accounts/new" for ::1 at 2019-04-29 12:11:33 -0500
Processing by AccountsController#new as HTML
Redirected to http://localhost:3000/login
Filter chain halted as :require_login rendered or redirected

I've noticed on the working branch that my GET uses the IP6 loopback. Here's the working one with IP4:

Started GET "/accounts/new" for 127.0.0.1 at 2019-04-29 13:40:51 -0500
Processing by AccountsController#new as HTML

Here's the broken one with IP6 (same as the first log snippet from above):

Started GET "/accounts/new" for ::1 at 2019-04-29 12:11:33 -0500
Processing by AccountsController#new as HTML
Redirected to http://localhost:3000/login
Filter chain halted as :require_login rendered or redirected

Would the loopback IP4 address 127.0.0.1 vs the loopback IP6 address ::1 cause this problem?

In my server log, I once got the error AbstractController::ActionNotFound (The action 'index' could not be found for Clearance::UsersController): but can't replicate it...

And it might be helpful to know that the app is setup for multi-tenancy with Pundit.

Thanks again for the help.

smcalilly avatar Apr 29 '19 19:04 smcalilly

earlier i closed the issue but it was premature. still having this issue

smcalilly avatar May 01 '19 03:05 smcalilly

I'm still new at Rails, so here is what I think is relevant my Accounts controller:

class AccountsController < ApplicationController
  before_action :set_account, only: [:show, :edit, :update, :destroy]
  before_action :restrict_access
  skip_before_action :require_account, only: [:new, :show, :create]
.
.
.
def show
    if current_user && current_user.account.blank?
      redirect_to new_account_path
    end
  end

  def new
    if current_user && current_user.account.present?
      redirect_to dashboard_path
    end

    @account = Account.new
  end

  def edit
  end

  def create
    @user    = current_user
    @account = @user.build_account(account_params)
  end
.
.
.
end

And the only thing in my Users controller is: skip_before_action :require_account

And some in my Application controller:

include Clearance::Controller
  before_action :require_login
  before_action :require_account

smcalilly avatar May 01 '19 15:05 smcalilly

@mjankowski I found that it works fine with Clearance version 1.16.0 but breaks with version 1.17.0. Don't know why. This is with Rails 6.0.0.rc1. Should I close the issue?

smcalilly avatar May 02 '19 15:05 smcalilly

Does it break with actual usage, or only with test coverage?

Are you using custom domains with clearance?

mjankowski avatar May 02 '19 15:05 mjankowski

It breaks on my tests and when I run my development server and go through the process manually via web browser.

I'm not sure what you mean by custom domains with clearance? I'm using a custom domain on my production. Here is my config/initializers/clearance.rb file:

Clearance.configure do |config|
  config.cookie_domain = ENV['COOKIE_DOMAIN']
  config.mailer_sender = ENV['SUPPORT_EMAIL']
  config.rotate_csrf_on_sign_in = true
  config.routes = false
  config.redirect_url = '/accounts/new'
end

smcalilly avatar May 02 '19 15:05 smcalilly

Might be related to previous issues - https://github.com/thoughtbot/clearance/issues/822 - https://github.com/thoughtbot/clearance/issues/821

Are you setting the COOKIE_DOMAIN env var in your test or development environments?

If/when you are setting that, you need to make sure that the domain name your tests are using (usually "example.com" with rack test) or the hostname you are using in development mode, match that value.

mjankowski avatar May 02 '19 16:05 mjankowski

Setting the cookie domain to default fixes the issue. Thanks.

smcalilly avatar May 13 '19 14:05 smcalilly