Advertisement
Guest User

Untitled

a guest
Sep 6th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. Model User
  2.  
  3. class User < ApplicationRecord
  4.  
  5. attr_accessor :registration
  6.  
  7. # Include default devise modules. Others available are:
  8. # :confirmable, :lockable, :timeoutable and :omniauthable
  9. devise :database_authenticatable, :registerable,
  10. :recoverable, :rememberable, :trackable, :validatable
  11. end
  12.  
  13. Controler Application
  14.  
  15. class ApplicationController < ActionController::Base
  16. attr_reader :current_user
  17.  
  18. private
  19. before_filter :configure_devise_params, if: :devise_controller?
  20. def configure_devise_params
  21. devise_parameter_sanitizer.for(:sign_up) do |u|
  22. u.permit(:registration, :email, :password, :password_confirmation)
  23. end
  24.  
  25. end
  26.  
  27. protected
  28. def authenticate_request!
  29. unless user_id_in_token?
  30. render json: { errors: ['Not Authenticated'] }, status: :unauthorized
  31. return
  32. end
  33. @current_user = User.find(auth_token[:user_id])
  34. rescue JWT::VerificationError, JWT::DecodeError
  35. render json: { errors: ['Not Authenticated'] }, status: :unauthorized
  36. end
  37.  
  38. private
  39. def http_token
  40. @http_token ||= if request.headers['Authorization'].present?
  41. request.headers['Authorization'].split(' ').last
  42. end
  43. end
  44.  
  45. def auth_token
  46. @auth_token ||= JsonWebToken.decode(http_token)
  47. end
  48.  
  49. def user_id_in_token?
  50. http_token && auth_token && auth_token[:user_id].to_i
  51. end
  52. end
  53.  
  54. u = User.new(email:'a@a.com', registration:192536, password:'changeme', password_confirmation:'changeme')
  55.  
  56. => #<User id: nil, email: "a@a.com", registration: nil>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement