1
I’m deploying token and authentication, I’m using JWT and Devise, I’m having a problem with Devise, I don’t want to use email for authentication, I want to use a license plate, so I read the Devise documentation and made the following adjustments, but the error still persists, it does not recognize the enrollment parameter when adding users.
Model User
class User < ApplicationRecord
attr_accessor :registration
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
Controler Application
class ApplicationController < ActionController::Base
attr_reader :current_user
private
before_filter :configure_devise_params, if: :devise_controller?
def configure_devise_params
devise_parameter_sanitizer.for(:sign_up) do |u|
u.permit(:registration, :email, :password, :password_confirmation)
end
end
protected
def authenticate_request!
unless user_id_in_token?
render json: { errors: ['Not Authenticated'] }, status: :unauthorized
return
end
@current_user = User.find(auth_token[:user_id])
rescue JWT::VerificationError, JWT::DecodeError
render json: { errors: ['Not Authenticated'] }, status: :unauthorized
end
private
def http_token
@http_token ||= if request.headers['Authorization'].present?
request.headers['Authorization'].split(' ').last
end
end
def auth_token
@auth_token ||= JsonWebToken.decode(http_token)
end
def user_id_in_token?
http_token && auth_token && auth_token[:user_id].to_i
end
end
In my bank I have already created the fields of registration
, email
, password
and password_confirmation
.
But when I try to create a user, however much I pass the data, it instance with null.
u = User.new(email:'[email protected]', registration:192536, password:'changeme', password_confirmation:'changeme')
=> #<User id: nil, email: "[email protected]", registration: nil>
Does anyone have any idea what it might be?
I was able to solve.... adding lines in the application controler before_action :configure_permitted_parameters, if: :devise_controller? #method that adds fields to Devise strong_paramsn, auth with email or matricula protected def configure_permitted_parameters added_attrs = [::Registration, :email, :password, :password_confirmation] devise_parameter_sanitizer.Permit :sign_up, Keys: added_attrs devise_parameter_sanitizer.Permit :account_update, Keys: added_attrs end
– Juliano
tried to remove ", :validatable"
– Otacilio