Module: YallaAuthRubyClient::ControllerAuthentication

Defined in:
lib/yalla_auth_ruby_client/controller_authentication.rb

Instance Method Summary collapse

Instance Method Details



8
9
10
# File 'lib/yalla_auth_ruby_client/controller_authentication.rb', line 8

def auth_token_cookie
  cookies.signed[:auth_token]
end

#authenticate_userObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/yalla_auth_ruby_client/controller_authentication.rb', line 12

def authenticate_user
  token = auth_token_cookie
  return false unless token.present?

  begin
    api_client = OpenapiClient::AuthApi.new
    response = api_client.auth_validate_token_get(token)

    if response && response.success
      @yalla_user = response.user
      @current_user = find_or_create_app_user(@yalla_user)
      (@current_user)
      true
    else
      cookies.delete(:auth_token)
      false
    end
  rescue OpenapiClient::ApiError => e
    Rails.logger.error "Authentication failed: #{e.message}"
    cookies.delete(:auth_token)
    false
  end
end

#authenticate_user!Object



36
37
38
39
40
# File 'lib/yalla_auth_ruby_client/controller_authentication.rb', line 36

def authenticate_user!
  return if authenticate_user

  redirect_to "#{ENV['AUTH_URL']}/users/sign_in?redirect_uri=#{request.original_url}", allow_other_host: true
end

#current_userObject



42
43
44
# File 'lib/yalla_auth_ruby_client/controller_authentication.rb', line 42

def current_user
  @current_user
end

#logoutObject



3
4
5
6
# File 'lib/yalla_auth_ruby_client/controller_authentication.rb', line 3

def logout
  cookies.delete(:auth_token, httponly: true)
  redirect_to ENV["AUTH_URL"], allow_other_host: true
end