Module: BetterAuth::Sinatra::Helpers

Defined in:
lib/better_auth/sinatra/helpers.rb

Instance Method Summary collapse

Instance Method Details

#authenticated?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/better_auth/sinatra/helpers.rb', line 18

def authenticated?
  !current_user.nil?
end

#current_sessionObject



8
9
10
11
# File 'lib/better_auth/sinatra/helpers.rb', line 8

def current_session
  data = better_auth_session_data
  data&.fetch(:session, nil) || data&.fetch("session", nil)
end

#current_userObject



13
14
15
16
# File 'lib/better_auth/sinatra/helpers.rb', line 13

def current_user
  data = better_auth_session_data
  data&.fetch(:user, nil) || data&.fetch("user", nil)
end

#require_authenticationObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/better_auth/sinatra/helpers.rb', line 22

def require_authentication
  return true if authenticated?

  if prefers_json_response?
    error = BetterAuth::APIError.new("UNAUTHORIZED")
    halt 401, {"content-type" => "application/json"}, JSON.generate(error.to_h)
  end

  halt 401, ""
end