Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 847
mtpbzz Avatar asked Nov 29 '25 23:11

mtpbzz


1 Answers

This is what you need to to in order to permit custom attributes for the Devise model (e.g 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

like image 169
Masroor Avatar answered Dec 02 '25 16:12

Masroor