Class: ApiEngineBase::ApplicationController

Inherits:
ActionController::API
  • Object
show all
Defined in:
app/controllers/api_engine_base/application_controller.rb

Constant Summary collapse

AUTHORIZATION_HEADER =
"AUTHORIZATION"

Instance Method Summary collapse

Instance Method Details

#add_to_bodyObject



40
41
42
43
44
45
# File 'app/controllers/api_engine_base/application_controller.rb', line 40

def add_to_body
  # {
  #   token_valid_till:,
  #   needs_email_verification:,
  # }
end

#authenticate_user!(bypass_email_validation: false) ⇒ Object

AUTHORIZATION_HEADER=“Bearer: value”



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/api_engine_base/application_controller.rb', line 9

def authenticate_user!(bypass_email_validation: false)
  raw_token = request.headers[AUTHORIZATION_HEADER]
  if raw_token.nil?
    status = 401
    schema = ApiEngineBase::Schema::Error::Base.new(status:, message: "Bearer token missing")
    render(json: schema.to_h, status:)
    return false
  end

  token = raw_token.split("Bearer:")[1].strip
  result = ApiEngineBase::Jwt::AuthenticateUser.(token:, bypass_email_validation:)
  if result.success?
    @current_user = result.user
    true
  else
    status = 401
    schema = ApiEngineBase::Schema::Error::Base.new(status:, message: result.msg)
    render(json: schema.to_h, status:)
    # Must return false so callbacks know to halt propagation
    false
  end
end

#authenticate_user_without_email_verification!Object



32
33
34
# File 'app/controllers/api_engine_base/application_controller.rb', line 32

def authenticate_user_without_email_verification!
  authenticate_user!(bypass_email_validation: true)
end

#current_userObject



36
37
38
# File 'app/controllers/api_engine_base/application_controller.rb', line 36

def current_user
  @current_user ||= nil
end