Rails设计令牌身份验证而不使用电子邮件

时间:2017-04-23 14:24:35

标签: ruby-on-rails ruby devise

我正在尝试在Rails 5.0.2上使用devise_token_auth gem和API app。 我通常按​​https://github.com/lynndylanhurley/devise_token_auth所述安装gem,因此添加到Gemfile,捆绑并安装在User模型上。问题是,我不想使用电子邮件进行身份验证(注册和登录)。我只想使用电话号码(当然包括密码和密码确认的POST)。

我添加了列:telephone_number,这对于users表是唯一的。并采取了必要的步骤来实现这一目标。 https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-with-something-other-than-their-email-address

所以这就是代码中的样子。

devise.rb

Devise.setup do |config|
   ...
   config.authentication_keys = [ :telephone_number ]
   config.case_insensitive_keys = [ :telephone_number ]
   config.strip_whitespace_keys = [ :telephone_number ]
   ...

user.rb

class User < ActiveRecord::Base
  # Include default devise modules.
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable, :authentication_keys => [:telephone_number]
  include DeviseTokenAuth::Concerns::User

  def email_required?
    false
  end
  def email_changed?
    false
  end

application_controller.rb

class ApplicationController < ActionController::API
  include DeviseTokenAuth::Concerns::SetUserByToken
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    added_attrs = [:telephone_number, :password, :password_confirmation, :remember_me]
    devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
    devise_parameter_sanitizer.permit :account_update, keys: added_attrs
  end
end

服务器正常启动,没有例外。但是,当我使用RESTfull服务POST on /auth测试注册时,它会给我验证错误,该电子邮件不能为空。

{"status":"error","data":{"id":null,"provider":"email","uid":"",
"name":null,"nickname":null,"image":null,"email":null,
"created_at":null,"updated_at":null,"telephone_number":"0123456789"},
"errors":{"email":["can't be blank","is not an email"],
"full_messages":["Email can't be blank","Email is not an email"]}}

编辑:

从Puma登录

    Started POST "/auth?=" for 127.0.0.1 at 2017-04-24 15:17:13 +0200
    Processing by DeviseTokenAuth::RegistrationsController#create as */*
      Parameters: {"telephone_number"=>"0123456789", "password"=>"[FILTERED]", 
      "password_confirmation"=>"[FILTERED]", "registration"=>{"telephone_number"=>"0123456789", 
      "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
    Unpermitted parameter: registration
    Unpermitted parameter: registration
    Unpermitted parameter: registration
       (0.3ms)  BEGIN
      SQL (0.9ms)  INSERT INTO "users" ("encrypted_password", "tokens", "created_at", 
     "updated_at", "telephone_number") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  
    [["encrypted_password", "$2a$10$PgCIPAGA1VH1erHvJX5Sg.f4IPmSyfSoVM3EVbKtHkbgeUvEGL5NG"], 
    ["tokens", "{}"], ["created_at", 2017-04-24 13:17:13 UTC], ["updated_at", 2017-04-24 13:17:13 UTC], 
    ["telephone_number", "0123456789"]]
       (0.2ms)  ROLLBACK
    Completed 422 Unprocessable Entity in 139ms (Views: 10.5ms | ActiveRecord: 1.3ms)

回复POST / auth

{"status":"error","data":{"id":null,"provider":"email","uid":"",
 "name":null,"nickname":null,"image":null,"email":null,
 "created_at":"2017-04-24T13:17:13.904Z","updated_at":"2017-04-24T13:17:13.904Z",
 "telephone_number":"0123456789"},"errors":["An account already exists for ''"]}

正在检查和验证电子邮件,当前是#34;&#34;,因此无法注册任何帐户。

我应该添加任何配置属性,还是覆盖某些方法?

提前谢谢。

0 个答案:

没有答案