设计宝石:仅使用电子邮件登录

时间:2017-03-08 18:59:53

标签: ruby-on-rails ruby authentication devise rubygems

我的问题很简单,¿我怎样才能通过客户的电子邮件进行身份验证(我的意思是根本没有密码)?我知道这是一个不好的做法,但这是客户的要求之一。

目前我已经完成了注册而没有密码,当它完成注册后,它会重定向到适当的页面(需要authenitcation的视图)。问题是,如果我删除cookie并尝试登录,则会显示“登录名或密码不正确”

目前我到目前为止:

用户模型:

class User < ApplicationRecord
   # Include default devise modules. Others available are:
   # :confirmable, :lockable, :timeoutable and :omniauthable

   devise :database_authenticatable, :registerable,
   :recoverable, :rememberable, :trackable,
   :validatable, :confirmable,:authentication_keys => [:login]

   attr_accessor :login

  def password_required?
     false
  end
end

Application_Controller:

class ApplicationController < ActionController::Base
   protect_from_forgery with: :exception
   before_action :configure_permitted_parameters, if: :devise_controller?

   protected

   def configure_permitted_parameters
     added_attrs = [:email, :remember_me]
     devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
     devise_parameter_sanitizer.permit :account_update, keys: added_attrs
     devise_parameter_sanitizer.permit :sign_in, keys: added_attrs
   end
end

用户迁移:

class DeviseCreateUsers < ActiveRecord::Migration[5.0]
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      t.string   :confirmation_token
      t.datetime :confirmed_at
      t.datetime :confirmation_sent_at
      t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_foreign_key :users, :doctores, column: :email, primary_key: :email
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end

显然,我已经更改了会话的新视图和注册的新视图

1 个答案:

答案 0 :(得分:0)

您是否看过这篇wiki文章? https://github.com/plataformatec/devise/wiki/How-To:-Email-only-sign-up

如果此处的意图是永远不会有密码,请注意第三部分 - 您可以覆盖设计password_required?password_match?