2017-03-08 54 views
0

我的問題很簡單,¿我如何才能通過客戶的電子郵件進行身份驗證(我的意思是根本沒有密碼)?我知道這是一種不好的做法,但這是客戶的要求之一。Devise Gem:只用電子郵件登錄

目前我已經完成註冊,沒有密碼,當它完成註冊後,它重定向到合適的頁面(需要驗證的視圖)。問題是,如果我刪除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 

很顯然,我alread變化會議的新觀點和登記的新觀點

+1

你見過這樣的職位? http://stackoverflow.com/questions/4662659/devise-authentication-without-password-using-just-username – gwalshington

+0

Nop我沒有,但我確實想過這件事。我在跳躍以獲得更「優雅」的解決方案。 – HFR1994

回答

0

你見過這個wiki文章? https://github.com/plataformatec/devise/wiki/How-To:-Email-only-sign-up

如果這裏的意圖是永遠不會有一個密碼,然後注意第三部分 - 你可以重寫設計的password_required?password_match?

+0

是的,我已經試過了,它只是定義它像def password_required?假結束和def password_match? true end – HFR1994

+0

您是否將用戶設置爲在註冊流程中的任意位置進行自動確認?他們手動確認?你需要「可確認」模塊嗎? – gwcodes

+0

是的,我沒有實現它,但我正在考慮這樣做 – HFR1994

相關問題