[英]how to properly authenticate user with devise_token_auth rails gem?
[英]How to permit params to a specific devise_controller with devise_token_auth gem?
我使用devise_token_authentication
gem構建基於令牌的身份驗證Rails api,然后在此之后,我通過不同的遷移向Vendor
模型添加了一些額外的字段,為了允許它們,我寫了這樣的代碼:
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, :tax_number])
devise_parameter_sanitizer.permit(:account_update, keys: [:first_name, :last_name, :tax_number])
end
end
然后在那之后,我添加了另一個模型Customer
rails g devise_token_auth:install Customer auth
然后在routes.rb中
Rails.application.routes.draw do
mount_devise_token_auth_for 'Vendor', at: 'vendor/auth'
mount_devise_token_auth_for 'Customer', at: 'customer/auth'
end
每次我嘗試通過'localhost:3000/customer/auth'
與客戶進行sign_up
,都會收到錯誤消息: ActiveModel::UnknownAttributeError: unknown attribute 'tax_number' for Customer.
那么,有什么方法可以只允許額外的字段用於Vendor
模型並跳過“客戶”?
要么
如果您覆蓋RegistrationsController
,則需要直接在registrationsController
允許額外的參數
class Users::RegistrationsController < DeviseTokenAuth::RegistrationsController
def create
end
def account_update
end
private
def sign_up_params
params.require(:user).permit(:email, :password, :password_confirmation, :first_name, :last_name, :tax_number)
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.