Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- u = User.new(email:'a@a.com', registration:192536, password:'changeme', password_confirmation:'changeme')
- => #<User id: nil, email: "a@a.com", registration: nil>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement