Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. # encoding: UTF-8
  2. # frozen_string_literal: true
  3.  
  4. require_dependency 'arke/middleware/jwt_authenticator'
  5.  
  6. class ApplicationController < ActionController::API
  7. before_action :auth_user!
  8. before_action :create_user
  9.  
  10. private
  11.  
  12. def current_user
  13. return @current_user if @current_user
  14.  
  15. if request.headers['Authorization']
  16. @auth ||= Arke::Middleware::JWTAuthenticator.new(pubkey: Rails.configuration.x.keystore.public_key)
  17. @current_user = @auth.before(request.headers)
  18. end
  19. end
  20.  
  21. def create_user
  22. render json: @current_user.errors, status: :not_found unless @current_user
  23.  
  24. @user = User.find_by(uid: @current_user[:uid])
  25.  
  26. if @user.nil?
  27. @user = User.create(current_user.slice(:uid, :email, :level, :role, :state))
  28. render json: @user.errors, status: :unprocessable_entity unless @user
  29. end
  30. end
  31.  
  32. def auth_user!
  33. unathorized unless current_user
  34. end
  35.  
  36. def unathorized
  37. render json: 'Unauthorized', status: 401
  38. end
  39. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement