Skip to content

Commit

Permalink
create customer service auth
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiomagalhaes committed Jun 5, 2024
1 parent 2a6e0d3 commit 5df92a2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
24 changes: 22 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,35 @@ class ApplicationController < ActionController::API
attr_reader :current_user

def authenticate
return user_invalid! unless authorization_header
if authorization_header
authenticate_user
elsif customer_service_auth_key
authenticate_customer_service
else
user_invalid!
end
end

private

def authenticate_user
user = User.find_or_initialize_by({
email: user_auth_params['email'],
google_id: user_auth_params['google_id']
})
return user_invalid! unless user.valid?

save_user!(user)

@current_user = user
end

def authenticate_customer_service
customer_service_auth = CustomerServiceAuth.find_by(auth_key: customer_service_auth_key)
return user_invalid! unless customer_service_auth

@current_user = customer_service_auth.customer
end

def save_user!(user)
if user.new_record?
user.first_name = user_auth_params['first_name']
Expand All @@ -45,6 +61,10 @@ def authorization_header
request.headers['Authorization']
end

def customer_service_auth_key
request.headers['Customer-Service-Auth-Key']
end

def user_auth_params
return @user_auth_params if @user_auth_params

Expand Down
20 changes: 19 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5df92a2

Please sign in to comment.