The personal finance app for everyone (by everyone)
Ruby
Eliminate code duplication in OIDC identity creation (#230)
Copilot1ee20ab3a6fa
diff --git a/app/controllers/oidc_accounts_controller.rb b/app/controllers/oidc_accounts_controller.rbindex 231790ab37..b6d7466bd5 100644--- a/app/controllers/oidc_accounts_controller.rb+++ b/app/controllers/oidc_accounts_controller.rb@@ -28,16 +28,9 @@ class OidcAccountsController < ApplicationControllerif user# Create the OIDC identity link- oidc_identity = user.oidc_identities.create!(- provider: @pending_auth["provider"],- uid: @pending_auth["uid"],- info: {- email: @pending_auth["email"],- name: @pending_auth["name"],- first_name: @pending_auth["first_name"],- last_name: @pending_auth["last_name"]- },- last_authenticated_at: Time.current+ oidc_identity = OidcIdentity.create_from_omniauth(+ build_auth_hash(@pending_auth),+ user)# Clear pending auth from session@@ -100,16 +93,9 @@ class OidcAccountsController < ApplicationControllerif @user.save# Create the OIDC identity- @user.oidc_identities.create!(- provider: @pending_auth["provider"],- uid: @pending_auth["uid"],- info: {- email: @pending_auth["email"],- name: @pending_auth["name"],- first_name: @pending_auth["first_name"],- last_name: @pending_auth["last_name"]- },- last_authenticated_at: Time.current+ OidcIdentity.create_from_omniauth(+ build_auth_hash(@pending_auth),+ @user)# Clear pending auth from session@@ -122,4 +108,15 @@ class OidcAccountsController < ApplicationControllerrender :new_user, status: :unprocessable_entityendend++ private++ # Convert pending auth hash to OmniAuth-like structure+ def build_auth_hash(pending_auth)+ OpenStruct.new(+ provider: pending_auth["provider"],+ uid: pending_auth["uid"],+ info: OpenStruct.new(pending_auth.slice("email", "name", "first_name", "last_name"))+ )+ endenddiff --git a/app/models/oidc_identity.rb b/app/models/oidc_identity.rbindex 12b8fd477e..78276546d0 100644--- a/app/models/oidc_identity.rb+++ b/app/models/oidc_identity.rb@@ -21,7 +21,8 @@ class OidcIdentity < ApplicationRecordname: auth.info&.name,first_name: auth.info&.first_name,last_name: auth.info&.last_name- }+ },+ last_authenticated_at: Time.current)endenddiff --git a/app/views/oidc_accounts/link.html.erb b/app/views/oidc_accounts/link.html.erbindex bfab765ec3..dbed9f2cd5 100644--- a/app/views/oidc_accounts/link.html.erb+++ b/app/views/oidc_accounts/link.html.erb@@ -42,21 +42,33 @@</div><div class="space-y-4">- <div class="p-4 bg-gray-50 border border-gray-200 rounded-md">- <p class="text-sm text-gray-700">+ <div class="p-4 bg-container border border-secondary rounded-md">+ <p class="text-sm text-primary"><strong>Email:</strong> <%= @pending_auth["email"] %></p><% if @pending_auth["name"].present? %>- <p class="text-sm text-gray-700 mt-2">+ <p class="text-sm text-primary mt-2"><strong>Name:</strong> <%= @pending_auth["name"] %></p><% end %></div>- <%= button_to "Create Account", create_user_oidc_account_path, method: :post, class: "w-full", data: { turbo: false } %>+ <%= render DS::Button.new(+ text: "Create Account",+ href: create_user_oidc_account_path,+ full_width: true,+ variant: :primary,+ method: :post,+ data: { turbo: false }+ ) %></div><% end %><div class="mt-6 text-center">- <%= link_to "Cancel", new_session_path, class: "font-medium text-sm text-primary hover:underline transition" %>+ <%= render DS::Link.new(+ text: "Cancel",+ href: new_session_path,+ variant: :default,+ class: "font-medium text-sm text-primary hover:underline transition"+ ) %></div>diff --git a/test/models/oidc_identity_test.rb b/test/models/oidc_identity_test.rbindex 09d83902b0..43342964b6 100644--- a/test/models/oidc_identity_test.rb+++ b/test/models/oidc_identity_test.rb@@ -77,5 +77,6 @@ class OidcIdentityTest < ActiveSupport::TestCaseassert_equal "[email protected]", identity.info["email"]assert_equal "Test User", identity.info["name"]assert_equal @user, identity.user+ assert_not_nil identity.last_authenticated_atendend