Skip to content

Commit cd2263f

Browse files
authored
Merge pull request #161 from immutable/feat/pkce-result
[DX-2624] feat: set pkce result if user is in the correct state
2 parents 0ff445c + ac60081 commit cd2263f

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public class PassportImpl
4343
internal static string loginPKCEUrl;
4444
#endif
4545

46+
// Used to prevent calling login/connect functions multiple times
47+
private bool isLoggedIn = false;
48+
4649
public event OnAuthEventDelegate OnAuthEvent;
4750

4851
public PassportImpl(IBrowserCommunicationsManager communicationsManager)
@@ -119,6 +122,7 @@ await ConfirmCode(
119122
PassportAuthEvent.LoginOpeningBrowser, PassportAuthEvent.PendingBrowserLogin, functionName,
120123
PassportFunction.LOGIN_CONFIRM_CODE, timeoutMs);
121124
SendAuthEvent(PassportAuthEvent.LoginSuccess);
125+
isLoggedIn = true;
122126
return true;
123127
}
124128
catch (Exception ex)
@@ -137,6 +141,7 @@ private async UniTask<bool> Relogin()
137141
string callResponse = await communicationsManager.Call(PassportFunction.RELOGIN);
138142
bool success = callResponse.GetBoolResponse() ?? false;
139143
SendAuthEvent(success ? PassportAuthEvent.ReloginSuccess : PassportAuthEvent.ReloginFailed);
144+
isLoggedIn = success;
140145
return success;
141146
}
142147
catch (Exception ex)
@@ -177,6 +182,7 @@ await ConfirmCode(
177182
PassportAuthEvent.ConnectImxOpeningBrowser, PassportAuthEvent.PendingBrowserLoginAndProviderSetup,
178183
functionName, PassportFunction.CONNECT_CONFIRM_CODE, timeoutMs);
179184
SendAuthEvent(PassportAuthEvent.ConnectImxSuccess);
185+
isLoggedIn = true;
180186
return true;
181187
}
182188
catch (Exception ex)
@@ -195,6 +201,7 @@ private async UniTask<bool> Reconnect()
195201
string callResponse = await communicationsManager.Call(PassportFunction.RECONNECT);
196202
bool success = callResponse.GetBoolResponse() ?? false;
197203
SendAuthEvent(success ? PassportAuthEvent.ReconnectSuccess : PassportAuthEvent.ReconnectFailed);
204+
isLoggedIn = success;
198205
return success;
199206
}
200207
catch (Exception ex)
@@ -276,8 +283,12 @@ public async void OnDeepLinkActivated(string url)
276283
if (domain.Equals(logoutRedirectUri))
277284
{
278285
await UniTask.SwitchToMainThread();
286+
if (isLoggedIn)
287+
{
288+
TrySetPKCEResult(true);
289+
}
279290
SendAuthEvent(PassportAuthEvent.LogoutPKCESuccess);
280-
TrySetPKCEResult(true);
291+
isLoggedIn = false;
281292
pkceCompletionSource = null;
282293
}
283294
else if (domain.Equals(redirectUri))
@@ -415,8 +426,12 @@ public async UniTask CompleteLoginPKCEFlow(string uriString)
415426
}
416427
else
417428
{
429+
if (!isLoggedIn)
430+
{
431+
TrySetPKCEResult(true);
432+
}
418433
SendAuthEvent(pkceLoginOnly ? PassportAuthEvent.LoginPKCESuccess : PassportAuthEvent.ConnectImxPKCESuccess);
419-
TrySetPKCEResult(true);
434+
isLoggedIn = true;
420435
}
421436
}
422437
}
@@ -437,7 +452,7 @@ public async UniTask CompleteLoginPKCEFlow(string uriString)
437452
public void OnLoginPKCEDismissed(bool completing)
438453
{
439454
Debug.Log($"{TAG} On Login PKCE Dismissed");
440-
if (!completing)
455+
if (!completing && !isLoggedIn)
441456
{
442457
// User hasn't entered all required details (e.g. email address) into Passport yet
443458
Debug.Log($"{TAG} Login PKCE dismissed before completing the flow");
@@ -484,6 +499,7 @@ public async UniTask Logout()
484499
string logoutUrl = await GetLogoutUrl();
485500
OpenUrl(logoutUrl);
486501
SendAuthEvent(PassportAuthEvent.LogoutSuccess);
502+
isLoggedIn = false;
487503
}
488504
catch (Exception ex)
489505
{
@@ -671,6 +687,7 @@ private async UniTask CallFromAuthCallbackError(string id, string message)
671687

672688
private void TrySetPKCEResult(bool result)
673689
{
690+
Debug.Log($"{TAG} Trying to set PKCE result to {result}...");
674691
if (pkceCompletionSource != null)
675692
{
676693
pkceCompletionSource.TrySetResult(result);
@@ -683,6 +700,7 @@ private void TrySetPKCEResult(bool result)
683700

684701
private void TrySetPKCEException(Exception exception)
685702
{
703+
Debug.Log($"{TAG} Trying to set PKCE exception...");
686704
if (pkceCompletionSource != null)
687705
{
688706
pkceCompletionSource.TrySetException(exception);
@@ -695,18 +713,20 @@ private void TrySetPKCEException(Exception exception)
695713

696714
private void TrySetPKCECanceled()
697715
{
716+
Debug.Log($"{TAG} Trying to set PKCE canceled...");
698717
if (pkceCompletionSource != null)
699718
{
700719
pkceCompletionSource.TrySetCanceled();
701720
}
702721
else
703722
{
704-
Debug.LogError($"{TAG} PKCE canceled");
723+
Debug.LogWarning($"{TAG} PKCE canceled");
705724
}
706725
}
707726

708727
private void SendAuthEvent(PassportAuthEvent authEvent)
709728
{
729+
Debug.Log($"{TAG} Send auth event: {authEvent}");
710730
if (OnAuthEvent != null)
711731
{
712732
OnAuthEvent.Invoke(authEvent);

0 commit comments

Comments
 (0)