加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 设计令牌身份验证 – 安装设备令牌authgem后无

发布时间:2020-12-17 03:42:00 所属栏目:百科 来源:网络整理
导读:我安装了devise token auth gem. https://github.com/lynndylanhurley/devise_token_auth#model-concerns 我设法获得路线和所有.但不幸的是,注册已停止工作. 当我尝试注册时,它会显示这样的错误. NoMethodError in Devise::RegistrationsController#create u
我安装了devise token auth gem. https://github.com/lynndylanhurley/devise_token_auth#model-concerns

我设法获得路线和所有.但不幸的是,注册已停止工作.

当我尝试注册时,它会显示这样的错误.

NoMethodError in Devise::RegistrationsController#create
    undefined method `provider' for #<User:0x007f49fd5da2e8>

 Extracted source (around line #433):

  else
    match = match_attribute_method?(method.to_s)
    match ? attribute_missing(match,*args,&block) : super
  end
end

这是我的路线

devise_for :admin_users,ActiveAdmin::Devise.config
   ActiveAdmin.routes(self)
   devise_for :users
   namespace :api do
     scope :v1 do
      mount_devise_token_auth_for 'User',at: 'auth'
     end
   end

根据文档,注册需要电子邮件,密码和password_confirmation.我不是我想念的.

这是“用户”的架构

create_table "users",force: :cascade do |t|
t.string   "email",default: "",null: false
t.string   "encrypted_password",null: false
t.string   "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
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"
t.datetime "created_at"
t.datetime "updated_at"
t.string   "authentication_token"
t.string   "provider",null: false
t.string   "uid",null: false
t.string   "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string   "unconfirmed_email"
t.string   "name"
t.string   "nickname"
t.string   "image"
t.string   "tokens"
end

   add_index "users",["authentication_token"],name: "index_users_on_authentication_token",using: :btree
   add_index "users",["email"],name: "index_users_on_email",unique: true,["reset_password_token"],name: "index_users_on_reset_password_token",["uid","provider"],name: "index_users_on_uid_and_provider",using: :btree

这是用户模型

class User < ActiveRecord::Base
      # Include default devise modules.
      devise :database_authenticatable,:registerable,:recoverable,:rememberable,:trackable,:validatable,:confirmable,:omniauthable
     include DeviseTokenAuth::Concerns::User
    has_many :movies
    has_many :reviews

   end

这是我用于创建用户帐户的POST请求?

{
    "user": {
    "email": "email@g.com","password": "password12345678","password_confirmation": "password12345678" }

     }

响应是这样的.

{"status":"error","errors":["Please submit proper sign up data in request body."]}

这是相应请求的日志.

Started POST "/api/v1/auth/" for 127.0.0.1 at 2015-05-28 15:51:28 +0530
Processing by DeviseTokenAuth::RegistrationsController#create as */*
  Parameters: {"user"=>{"email"=>"email@g.com","password"=>"[FILTERED]","password_confirmation"=>"[FILTERED]"},"registration"=>{"user"=>{"email"=>"email@g.com","password_confirmation"=>"[FILTERED]"}}}
Can't verify CSRF token authenticity
Unpermitted parameters: user,registration
Filter chain halted as :validate_sign_up_params rendered or redirected
Completed 422 Unprocessable Entity in 4ms (Views: 0.3ms | ActiveRecord: 0.0ms)

该错误背后的问题是什么,我该如何解决?

解决方法

不知何故,我设法找出了实施中遗漏的内容.当我使用电子邮件身份验证时,设计令牌auth gem需要uid和提供者.

当我们尝试在没有这些参数的情况下发布请求时,由于参数验证,它将失败.为了绕过它,我们可以在user方法中使用before_validation生成一个uid和一个提供者.

如果提供者是空白的,我在这里使用电子邮件作为提供者;对于uid,如果它是空白的,我使用电子邮件.

这是我在用户模型中写的.

before_validation :set_provider
  before_validation :set_uid

  def set_provider
    self[:provider] = "email" if self[:provider].blank?
  end

  def set_uid
    self[:uid] = self[:email] if self[:uid].blank? && self[:email].present?
  end

通过这种方式,您将获得用户注册所需的参数.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读