mirror of
https://github.com/internetee/registry.git
synced 2025-06-08 13:44:47 +02:00
Create base controller class
This commit is contained in:
parent
65676ae637
commit
8f234a5852
5 changed files with 67 additions and 23 deletions
31
app/controllers/api/v1/registrant/base_controller.rb
Normal file
31
app/controllers/api/v1/registrant/base_controller.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
require 'rails5_api_controller_backport'
|
||||
require 'auth_token/auth_token_decryptor'
|
||||
|
||||
module Api
|
||||
module V1
|
||||
module Registrant
|
||||
class BaseController < ActionController::API
|
||||
before_action :authenticate
|
||||
|
||||
private
|
||||
|
||||
def bearer_token
|
||||
pattern = /^Bearer /
|
||||
header = request.headers['Authorization']
|
||||
header.gsub(pattern, '') if header && header.match(pattern)
|
||||
end
|
||||
|
||||
def authenticate
|
||||
decryptor = AuthTokenDecryptor.create_with_defaults(bearer_token)
|
||||
decryptor.decrypt_token
|
||||
|
||||
if decryptor.valid?
|
||||
sign_in decryptor.user
|
||||
else
|
||||
render json: { error: 'Not authorized' }, status: 403
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue