Skip to content

Commit

Permalink
feat: [#3379] Add User login/creation BOS hook
Browse files Browse the repository at this point in the history
  • Loading branch information
wujuu committed Jan 30, 2025
1 parent b82db14 commit 270bde2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ We are currently using the following ENV variables:
or `Rails.application.credentials.checkin[:identifier]`)
- `SHOW_RECOMMENDATION_PANEL` - Boolean (true/false) indicating
if recommendation panel should be visible or not. Defaults to true

- `BOS_URL` - URL of the Backend Ordering System backend (default: `http://localhost:8000`)
## Commits

Running `./bin/setup` automatically installs githooks
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def checkin
@user = User::Checkin.from_omniauth(auth)

if @user.persisted?
User::SyncWithBos.sync(@user)

sign_in_and_redirect @user, event: :authentication, allow_other_host: true
if cookies[:favourites].present?
Array(cookies[:favourites].split("&")).each do |favourite|
Expand Down
19 changes: 19 additions & 0 deletions app/models/user/sync_with_bos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

class User::SyncWithBos
def self.sync(user)
user_params = { email: user.email, name: user.full_name, user_type: ["mp_user"] }

response =
Faraday.post("#{Mp::Application.config.bos_url}/users/") do |req|
req.headers["Content-Type"] = "application/json"
req.body = user_params.to_json
end

body = JSON.parse(response.body)
Rails.logger.warn("User `#{user.email}` already exists in BOS.") if response.status == 409
Rails.logger.error("Failed to sync user `#{user.email}` with BOS. Response: #{body}") if response.status != 200
rescue StandardError => e
Rails.logger.error("Failed to reach the BOS endpoint at #{Mp::Application.config.bos_url}: #{e.message}")
end
end
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,7 @@ class Application < Rails::Application
config.enable_external_search = ActiveModel::Type::Boolean.new.cast(ENV.fetch("MP_ENABLE_EXTERNAL_SEARCH", false))

config.whitelabel = ENV.fetch("MP_WHITELABEL", false)

config.bos_url = ENV["BOS_URL"].present? ? ENV["BOS_URL"] : "http://localhost:8000"
end
end

0 comments on commit 270bde2

Please sign in to comment.