ruby-on-railsrubydevise-token-auth

Adding extra fields to devise token auth user sign up


I have a signup form for devise-token-auth. I would like users to enter a username as well on the signup form. The username column exists in the database but the controller rejects it

Unpermitted parameter: :username

How can I extend the permitted parameters for signup in devise token auth?


Solution

  • Looks like this works exactly the same as regular devise. The general idea can be found from this page

    But specifically, add this to ApplicationController

    before_action :configure_permitted_parameters, if: :devise_controller?
    
    protected
    
    def configure_permitted_parameters
      added_attrs = [:username, :email, :password, :password_confirmation]
      devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
      devise_parameter_sanitizer.permit :account_update, keys: added_attrs
    end