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 70e3bcd
Show file tree
Hide file tree
Showing 7 changed files with 148 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
32 changes: 32 additions & 0 deletions app/models/customer_service_auth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: customer_service_auths
#
# id :bigint not null, primary key
# auth_key :string
# created_at :datetime not null
# updated_at :datetime not null
# customer_id :bigint not null
#
# Indexes
#
# index_customer_service_auths_on_customer_id (customer_id)
#
# Foreign Keys
#
# fk_rails_... (customer_id => customers.id)
#

class CustomerServiceAuth < ApplicationRecord
belongs_to :customer

before_create :generate_auth_key

private

def generate_auth_key
self.auth_key = SecureRandom.hex(10) # Generates a random hex string of 20 characters
end
end
12 changes: 12 additions & 0 deletions db/migrate/20240605181342_create_customer_api_auth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class CreateCustomerApiAuth < ActiveRecord::Migration[7.0]
def change
create_table :customer_api_auths do |t|
t.string :auth_key
t.references :customer, null: false, foreign_key: true

t.timestamps
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20240605181504_create_customer_service_auths.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class CreateCustomerServiceAuths < ActiveRecord::Migration[7.0]
def change
create_table :customer_service_auths do |t|
t.string :auth_key
t.references :customer, null: false, foreign_key: true

t.timestamps
end
end
end
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.

26 changes: 26 additions & 0 deletions spec/factories/customer_service_auths.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: customer_service_auths
#
# id :bigint not null, primary key
# auth_key :string
# created_at :datetime not null
# updated_at :datetime not null
# customer_id :bigint not null
#
# Indexes
#
# index_customer_service_auths_on_customer_id (customer_id)
#
# Foreign Keys
#
# fk_rails_... (customer_id => customers.id)
#
FactoryBot.define do
factory :customer_service_auth do
auth_key { 'MyString' }
customer { nil }
end
end
25 changes: 25 additions & 0 deletions spec/models/customer_service_auth_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: customer_service_auths
#
# id :bigint not null, primary key
# auth_key :string
# created_at :datetime not null
# updated_at :datetime not null
# customer_id :bigint not null
#
# Indexes
#
# index_customer_service_auths_on_customer_id (customer_id)
#
# Foreign Keys
#
# fk_rails_... (customer_id => customers.id)
#
require 'rails_helper'

RSpec.describe CustomerServiceAuth, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit 70e3bcd

Please sign in to comment.