From e15b67da29ab189a480588a30d86c30360b0d718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enes=20=C3=87ak=C4=B1r?= Date: Fri, 27 Dec 2024 18:25:13 +0300 Subject: [PATCH] Retrieve `omniauth_identity` automatically when accessed for first time (#29) I often forget to call `retrieve_omniauth_identity` before trying to access the `omniauth_identity` attribute in `before_omniauth_callback_route`. I think we can make it automatically retrieve `omniauth_identity` the first time it's called. I haven't removed the `retrieve_omniauth_identity` method since it might already be called by users. --- lib/rodauth/features/omniauth.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/rodauth/features/omniauth.rb b/lib/rodauth/features/omniauth.rb index a40050f..99abe62 100644 --- a/lib/rodauth/features/omniauth.rb +++ b/lib/rodauth/features/omniauth.rb @@ -58,14 +58,12 @@ def handle_omniauth_callback def _handle_omniauth_callback before_omniauth_callback_route - retrieve_omniauth_identity - - if !account && omniauth_identity - account_from_omniauth_identity - end - unless account - account_from_omniauth + if omniauth_identity + account_from_omniauth_identity + else + account_from_omniauth + end end if account && !open_account? @@ -100,8 +98,12 @@ def _handle_omniauth_callback end end + def omniauth_identity + @omniauth_identity ||= retrieve_omniauth_identity + end + def retrieve_omniauth_identity - @omniauth_identity = _retrieve_omniauth_identity(omniauth_provider, omniauth_uid) + _retrieve_omniauth_identity(omniauth_provider, omniauth_uid) end def account_from_omniauth_identity @@ -142,8 +144,6 @@ def after_close_account remove_omniauth_identities end - attr_reader :omniauth_identity - def omniauth_second_factor? features.include?(:two_factor_base) && uses_two_factor_authentication? && omniauth_two_factors? end