首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Authlogic::Session::Activation::NotActivatedError

Authlogic::Session::Activation::NotActivatedError
EN

Stack Overflow用户
提问于 2021-03-01 09:33:09
回答 1查看 251关注 0票数 1

使用authlogic在浏览器中验证用户登录,但获得“您的帐户在非活动错误”,我已经尝试了所有的解决方案,我可以在网上找到,但它们似乎都没有工作。

在创建会话之前调用命令Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(controller)可以解决这个问题,至少在irb中是这样。

UserSessionsController剂量上做同样的事情能让你找到任何地方。我尝试在before_action :activate_authlogic中调用UserSessionsController,而将该函数的定义放在application_controller.rb中。

这是我的密码

user_sessions_controller.rb

代码语言:javascript
运行
复制
class   UserSessionsController < ApplicationController
  def new
    @user_session = UserSession.new
  end

  def create
    @user_session = UserSession.new(user_session_params.to_h)
    if @user_session.save
      redirect_to root_url
    else
      render :action => :new
    end
  end

  def destroy
    current_user_session.destroy
    redirect_to new_user_session_url
  end

  private

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

end

user_session.rb

代码语言:javascript
运行
复制
class UserSession < Authlogic::Session::Base
end

application_controller.rb

代码语言:javascript
运行
复制
class ApplicationController < ActionController::Base
  helper_method :current_user_session, :current_user

  private
    def current_user_session
      return @current_user_session if defined?(@current_user_session)
      @current_user_session = UserSession.find
    end

    def current_user
      return @current_user if defined?(@current_user)
      @current_user = current_user_session && current_user_session.user
    end

    # def activate_authlogic
    #   Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
    # end
end

user.rb

代码语言:javascript
运行
复制
class User < ApplicationRecord
  has_one :user_profile

  acts_as_authentic do |c|
    c.crypto_provider = ::Authlogic::CryptoProviders::SCrypt
  end

  validates :email,
    format: {
      with: /@/,
      message: "should look like an email address."
    },
    length: { maximum: 100 },
    uniqueness: {
      case_sensitive: false,
      if: :will_save_change_to_email?
    }

  validates :login,
    format: {
      with: /\A[a-z0-9]+\z/,
      message: "should use only letters and numbers."
    },
    length: { within: 8..100 },
    uniqueness: {
      case_sensitive: false,
      if: :will_save_change_to_login?
    }

  validates :password,
    confirmation: { if: :require_password? },
    length: {
      minimum: 8,
      if: :require_password?
    }
  validates_presence_of :password_confirmation
    length: {
      minimum: 8,
      if: :require_password?
  }
end

以防万一

routes.rb

代码语言:javascript
运行
复制
Rails.application.routes.draw do
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  root "home_page#index"
  get "/home", to: "home_page#index"

  resources :users do
    resource :user_profile
  end

  resource :user_session
end

我确实认为这是一个bug,所以我从6.4.0将authlogic更新为6.4.1。不用说,这也于事无补。

下面是

Gemfile

代码语言:javascript
运行
复制
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.2'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.1.1'
# Use postgresql as the database for Active Record
gem 'pg', '~> 1.1'
# Use Puma as the app server
gem 'puma', '~> 5.0'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 5.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'authlogic'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.4', require: false

gem "scrypt", "~> 3.0"

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 4.1.0'
  # Display performance information such as SQL time and flame graphs for each request in your browser.
  # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md
  gem 'rack-mini-profiler', '~> 2.0'

  gem 'rspec-rails', '~> 4.0.2'

  gem 'capybara'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

new.html.erb (表格)

代码语言:javascript
运行
复制
<%= form_for @user_session, url: user_session_url do |f| %>
  <% if @user_session.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@user_session.errors.count, "error") %> prohibited:</h2>
    <ul>
      <% @user_session.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
  <% end %>
  <%= f.label :login %><br />
  <%= f.text_field :login %><br />
  <br />
  <%= f.label :password %><br />
  <%= f.password_field :password %><br />
  <br />
  <%= f.label :remember_me %><br />
  <%= f.check_box :remember_me %><br />
  <br />
  <%= f.submit "Login" %>
<% end %>

迁移表

代码语言:javascript
运行
复制
class CreateUsers < ActiveRecord::Migration[6.1]
  def change
    create_table :users do |t|
      # Authlogic::ActsAsAuthentic::Email
      t.string    :email
      t.index     :email, unique: true

      # Authlogic::ActsAsAuthentic::Login
      t.string    :login

      # Authlogic::ActsAsAuthentic::Password
      t.string    :crypted_password
      t.string    :password_salt

      # Authlogic::ActsAsAuthentic::PersistenceToken
      t.string    :persistence_token
      t.index     :persistence_token, unique: true

      # Authlogic::ActsAsAuthentic::SingleAccessToken
      t.string    :single_access_token
      t.index     :single_access_token, unique: true

      # Authlogic::ActsAsAuthentic::PerishableToken
      t.string    :perishable_token
      t.index     :perishable_token, unique: true

      # See "Magic Columns" in Authlogic::Session::Base
      t.integer   :login_count, default: 0, null: false
      t.integer   :failed_login_count, default: 0, null: false
      t.datetime  :last_request_at
      t.datetime  :current_login_at
      t.datetime  :last_login_at
      t.string    :current_login_ip
      t.string    :last_login_ip

      # See "Magic States" in Authlogic::Session::Base
      t.boolean   :active, default: false
      t.boolean   :approved, default: false
      t.boolean   :confirmed, default: false

      t.timestamps
    end
  end
end
EN

回答 1

Stack Overflow用户

发布于 2021-03-06 04:00:16

Your account in not active error,这是因为迁移文件添加了Magic States字段:

代码语言:javascript
运行
复制
# See "Magic States" in Authlogic::Session::Base
t.boolean   :active, default: false
t.boolean   :approved, default: false
t.boolean   :confirmed, default: false

这意味着在登录用户之前,必须将这些字段设置为true

因此,如果不需要,可以在新迁移中删除这些字段。

代码语言:javascript
运行
复制
def change
  change_table :users do |t|
    t.remove :title, :active
    t.remove :title, :approved
    t.remove :title, :confirmed
  end
end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66419617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档