设计令牌身份验证 - 安装设计令牌 authgem 后无法创建用户帐户
Devise token auth - Can't create user account after installing devise token authgem
我安装了 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。我不是我在这里想念的人。
这里是 'User'
的架构
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", 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", default: "", 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, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
add_index "users", ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true, 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"=>"[FILTERED]", "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)
该错误背后的问题是什么,我该如何解决?
不知何故,我设法弄清楚了实施中遗漏了什么。当我使用电子邮件身份验证时,设计令牌身份验证 gem 需要 uid
和 provider
。
当我们尝试 post 没有这些参数的请求时,它将因参数验证而失败。为了绕过它,我们可以在用户方法中使用 before_validation
生成一个 uid
和一个 provider
。
这里如果provider
为空我就用email
作为provider
,对于uid
如果是空我就用email
。
这是我在用户模型中写的。
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
通过这种方式,您将获得用户注册所需的参数。
我通过从 :user 子哈希中取出用户参数来解决这个问题。我正在写一个控制器测试
post :create, { email: "user@example.org", password: "password", password_confirmation: "password" }
工作并且
post :create, {user: { email: "user@example.org", password: "password", password_confirmation: "password" }}
您还可以使用 Devise 生成的令牌设置 uid
。
Devise.friendly_token.first(10)
我安装了 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。我不是我在这里想念的人。
这里是 'User'
的架构create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", 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", default: "", 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, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
add_index "users", ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true, 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"=>"[FILTERED]", "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)
该错误背后的问题是什么,我该如何解决?
不知何故,我设法弄清楚了实施中遗漏了什么。当我使用电子邮件身份验证时,设计令牌身份验证 gem 需要 uid
和 provider
。
当我们尝试 post 没有这些参数的请求时,它将因参数验证而失败。为了绕过它,我们可以在用户方法中使用 before_validation
生成一个 uid
和一个 provider
。
这里如果provider
为空我就用email
作为provider
,对于uid
如果是空我就用email
。
这是我在用户模型中写的。
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
通过这种方式,您将获得用户注册所需的参数。
我通过从 :user 子哈希中取出用户参数来解决这个问题。我正在写一个控制器测试
post :create, { email: "user@example.org", password: "password", password_confirmation: "password" }
工作并且
post :create, {user: { email: "user@example.org", password: "password", password_confirmation: "password" }}
您还可以使用 Devise 生成的令牌设置 uid
。
Devise.friendly_token.first(10)