Devise / Rails登录无法正常工作 - “无效的电子邮件或密码”

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Devise / Rails登录无法正常工作 - “无效的电子邮件或密码”相关的知识,希望对你有一定的参考价值。

我和Devise有问题。我正在尝试在Rails中设置身份验证方案,当我尝试登录时,我收到“无效的电子邮件或密码”响应。这是在创建用户,登录,注销,然后尝试再次登录。我完全相信电子邮件/密码匹配,所以这不是问题。我正在使用Devise 4.4.3和Rails 5.2.0。请帮帮忙!

用户控制器:

    class UsersController < ApplicationController
      before_action :authenticate_user!

      def new
        @user = User.new
      end

      def create
        @user = User.new(user_params)
        if @user.save
          flash[:notice] = "You've successfully signed up!"
        else
          flash[:alert] = "There was a problem signing up."
          redirect_to '/signup'
        end
      end

    private

      def user_params
        params.require(:user).permit(:email, :password, :password_confirmation)
      end
    end

会话控制器:

    class SessionsController < ApplicationController
      before_action :authenticate_user!

      def create
        @user = User.authenticate(params[:email], params[:password])
        if @user
          flash[:notice] = "You've signed in."
          session[:user_id] = @user.id
          redirect_to "/"
        else
          flash[:alert] = "There was a problem signing in. Please try again."
          redirect_to signin_path
        end
      end
    end

用户模型:

    class User < ActiveRecord::Base
      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable
      has_many :posts
      has_many :projects
      attr_accessor :password
      validates_confirmation_of :password
      validates :email, :presence => true, :uniqueness => true
      before_save :encrypt_password

      def encrypt_password
        self.password_salt = BCrypt::Engine.generate_salt
        self.password_hash = BCrypt::Engine.hash_secret(password,password_salt)
      end

      def self.authenticate(email, password)
        user = User.find_by "email = ?", email
        if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
          user
        else
          nil
        end
      end
    end


    <h2>Log in</h2>

    <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
      <div class="field">
        <%= f.label :email %><br />
        <%= f.email_field :email, autofocus: true, autocomplete: "email" %>
      </div>

      <div class="field">
        <%= f.label :password %><br />
        <%= f.password_field :password, autocomplete: "off" %>
      </div>

      <% if devise_mapping.rememberable? -%>
        <div class="field">
          <%= f.check_box :remember_me %>
          <%= f.label :remember_me %>
        </div>
      <% end -%>

      <div class="actions">
        <%= f.submit "Log in" %>
      </div>
    <% end %>

    <%= render "devise/shared/links" %>

的routes.rb

    Rails.application.routes.draw do
      devise_for :users
      root :to => 'homes#index'

      resources :posts
      resources :projects
      resources :users
    end

新会话视图:

    <h2>Log in</h2>

    <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
      <div class="field">
        <%= f.label :email %><br />
        <%= f.email_field :email, autofocus: true, autocomplete: "email" %>
      </div>

      <div class="field">
        <%= f.label :password %><br />
        <%= f.password_field :password, autocomplete: "off" %>
      </div>

      <% if devise_mapping.rememberable? -%>
        <div class="field">
          <%= f.check_box :remember_me %>
          <%= f.label :remember_me %>
        </div>
      <% end -%>

      <div class="actions">
        <%= f.submit "Log in" %>
      </div>
    <% end %>

    <%= render "devise/shared/links" %>

如果需要,很乐意提供更多。如果这是一个非常明显的答案,我仍然对开发有点新意,请原谅我。谢谢!

答案

在用户模型中试试这个

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

以上是关于Devise / Rails登录无法正常工作 - “无效的电子邮件或密码”的主要内容,如果未能解决你的问题,请参考以下文章

Rails/Devise 登录在 Safari 上不起作用(422/CSRF 错误)

防止在rails / devise中没有邀请代码的情况下登录

Rails Devise Omniauth new_user_registration_url

Rails & Devise:如何在没有布局的情况下呈现登录页面?

如何让 Devise 和 JWT 与我的版本化 API Rails 控制器一起工作?

如何从 Rails 控制台使用 Devise 登录用户?