简体   繁体   中英

Custom user attributes with devise_token_auth

I have created a new API using rails/edge and I'm using devise with the devise_token_auth which has generated me a user model.

My question is this, after migrating and adding new attributes to the User model how do I return this in the JSON response, currently being generated by devise_token_auth?

This is what you need to to in order to permit custom attributes for the Devise model (eg User model)

class ApplicationController < ActionController::Base    
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    # for user account creation i.e sign up
    devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name])
    # for user account update
    # where bank_name and bank_account are nested attributes in the User model
    devise_parameter_sanitizer.permit(:account_update, keys: [:first_name, :last_name, bank_attributes: [:bank_name, :bank_account]])
  end
end

source of this solution

As mentioned in the comment above. So far my best solution is to create a separate UserDetails model, serialize that then make a separate call to it after auth to populate the extra user data.

Which does actually seem quite nice as it keeps the auth clean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM