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 database 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 instances 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 an idea what it can be?