Search code examples
ruby-on-railsauthlogicruby-on-rails-5

rails 5 authlogic cryptic user_session error


I'm trying to set up Authlogic with Rails 5 (hence the user_session_params.to_h), and when I try to create a new session, I get the following error message with which I don't know what to do. Any suggestions will be much appreciated.

puts @user_session.errors.inspect

#<Authlogic::Session::Validation::Errors:0x0000000cb9b7a0 @base=#<UserSession: {:email=>"[email protected]", :password=>"<protected>"}>, @messages={:base=>["You did not provide any details for authentication."]}, @details={:base=>[{:error=>"You did not provide any details for authentication."}]}>

Here's the code:

UserSessionsController

class UserSessionsController < ApplicationController
  def new
    @user_session = UserSession.new
  end

  def create
    @user_session = UserSession.new(user_session_params.to_h)
    puts @user_session.errors.inspect

    if @user_session.save
      flash[:notice] = "Login successful, thank you!"
      redirect_to users_path
    else
      flash[:notice] = "Something went wrong, sorry."
    end
  end

  def destroy
    current_user_session.destroy
    redirect_to new_user_sessions_path
  end

  private

  def user_session_params
    params.require(:user_session).permit(:email, :password, :remember_me)
  end
end

UserSession

class UserSession < Authlogic::Session::Base
end

View

<%= form_for @user_session, url: user_sessions_path, method: :post, html: {class: 'form-horizontal', role: 'form'} do |f| %>
    <div class='form-group'>
      <%= f.email_field :email, class: 'form-control', placeholder: 'Login' %>
    </div>
    <div class='form-group'>
      <%= f.password_field :password, class: 'form-control', placeholder: 'Password' %>
    </div>
    <%= f.submit 'Login', class: 'btn btn-primary' %>
<% end %>

User

class User < ApplicationRecord
    acts_as_authentic do |c|
        c.login_field = 'email'
    end
end

Solution

  • There seems to be an issue of Authlogic under Rails 5.

    You've probably already seen this comment from github issue 487 but you probably followed only the first step. The step nr. 2 deals with broken Authlogic callbacks under Rails 5 and there is a link to pull request #488 where a fix is developed. Currently, your only options seem to be the following:

    • You can try this branch of Authlogic that allegedly has the callbacks issue fixed, as commented in the pull request 488.
    • You can of course wait until things settle down and Authlogic gets an official support for Rails5.