Module: UffizziCore::AuthManagement

Included in:
ApplicationController
Defined in:
app/controllers/concerns/uffizzi_core/auth_management.rb

Instance Method Summary collapse

Instance Method Details

#auth_tokenObject



20
21
22
23
# File 'app/controllers/concerns/uffizzi_core/auth_management.rb', line 20

def auth_token
  header = request.headers['Authorization']
  header&.split(' ')&.last
end

#authenticate_request!Object



36
37
38
# File 'app/controllers/concerns/uffizzi_core/auth_management.rb', line 36

def authenticate_request!
  current_user ? true : head(:unauthorized)
end

#current_userObject



16
17
18
# File 'app/controllers/concerns/uffizzi_core/auth_management.rb', line 16

def current_user
  @current_user ||= UffizziCore::User.find_by(id: current_user_id)
end

#current_user_idObject



25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/concerns/uffizzi_core/auth_management.rb', line 25

def current_user_id
  return session[:user_id] if session[:user_id].present?
  return unless auth_token.present?

  decoded_token = UffizziCore::TokenService.decode(auth_token)
  return unless decoded_token
  return if decoded_token.first['expires_at'] < DateTime.now

  decoded_token.first['user_id']
end

#sign_in(user) ⇒ Object



4
5
6
# File 'app/controllers/concerns/uffizzi_core/auth_management.rb', line 4

def (user)
  session[:user_id] = user.id
end

#sign_outObject



8
9
10
# File 'app/controllers/concerns/uffizzi_core/auth_management.rb', line 8

def sign_out
  session[:user_id] = @current_user = nil
end

#signed_in?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'app/controllers/concerns/uffizzi_core/auth_management.rb', line 12

def signed_in?
  session[:user_id].present? && current_user.present?
end