Skip to content

Instantly share code, notes, and snippets.

@chumaknadya
Created July 12, 2019 13:56
Show Gist options
  • Save chumaknadya/e7654ccc6408baeae5e4bfe975b6993d to your computer and use it in GitHub Desktop.
Save chumaknadya/e7654ccc6408baeae5e4bfe975b6993d to your computer and use it in GitHub Desktop.
# encoding: UTF-8
# frozen_string_literal: true
require_dependency 'arke/middleware/jwt_authenticator'
class ApplicationController < ActionController::API
before_action :auth_user!
before_action :create_user
private
def current_user
return @current_user if @current_user
if request.headers['Authorization']
@auth ||= Arke::Middleware::JWTAuthenticator.new(pubkey: Rails.configuration.x.keystore.public_key)
@current_user = @auth.before(request.headers)
end
end
def create_user
render json: @current_user.errors, status: :not_found unless @current_user
@user = User.find_by(uid: @current_user[:uid])
if @user.nil?
@user = User.create(current_user.slice(:uid, :email, :level, :role, :state))
render json: @user.errors, status: :unprocessable_entity unless @user
end
end
def auth_user!
unathorized unless current_user
end
def unathorized
render json: 'Unauthorized', status: 401
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment