Skip to content

Commit

Permalink
feat: ability to skip logging out of auth0 in the browser
Browse files Browse the repository at this point in the history
  • Loading branch information
nattb8 committed Feb 29, 2024
1 parent 7d0a15d commit 06cea50
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
25 changes: 17 additions & 8 deletions src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,14 +520,17 @@ public async UniTask<string> GetLogoutUrl()
}
}

public async UniTask Logout()
public async UniTask Logout(bool hardLogout = true)
{
try
{
SendAuthEvent(PassportAuthEvent.LoggingOut);

string logoutUrl = await GetLogoutUrl();
OpenUrl(logoutUrl);
if (hardLogout)
{
OpenUrl(logoutUrl);
}

Track(PassportAnalytics.EventName.COMPLETE_LOGOUT, success: true);
SendAuthEvent(PassportAuthEvent.LogoutSuccess);
Expand All @@ -544,15 +547,15 @@ public async UniTask Logout()
}
}

public UniTask LogoutPKCE()
public UniTask LogoutPKCE(bool hardLogout = true)
{
try
{
SendAuthEvent(PassportAuthEvent.LoggingOutPKCE);

UniTaskCompletionSource<bool> task = new UniTaskCompletionSource<bool>();
pkceCompletionSource = task;
LaunchLogoutPKCEUrl();
LaunchLogoutPKCEUrl(hardLogout);
return task.Task;
}
catch (Exception ex)
Expand All @@ -579,15 +582,21 @@ private async void HandleLogoutPKCESuccess()
pkceCompletionSource = null;
}

private async void LaunchLogoutPKCEUrl()
private async void LaunchLogoutPKCEUrl(bool hardLogout)
{
string logoutUrl = await GetLogoutUrl();

if (hardLogout)
{
#if UNITY_ANDROID && !UNITY_EDITOR
LaunchAndroidUrl(logoutUrl);
LaunchAndroidUrl(logoutUrl);
#else
communicationsManager.LaunchAuthURL(logoutUrl, logoutRedirectUri);
communicationsManager.LaunchAuthURL(logoutUrl, logoutRedirectUri);
#endif
}
else
{
HandleLogoutPKCESuccess();
}
}

public async UniTask<bool> HasCredentialsSaved()
Expand Down
10 changes: 6 additions & 4 deletions src/Packages/Passport/Runtime/Scripts/Public/Passport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,20 @@ public async UniTask<string> GetAddress()
/// Logs the user out of Passport and removes any stored credentials.
/// Recommended to use when logging in using device auth flow - ConnectImx()
/// </summary>
public async UniTask Logout()
/// <param name="hardLogout">If false, the user will not be logged out of Passport in the browser. The default is true.</param>
public async UniTask Logout(bool hardLogout = true)
{
await GetPassportImpl().Logout();
await GetPassportImpl().Logout(hardLogout);
}

/// <summary>
/// Logs the user out of Passport and removes any stored credentials.
/// Recommended to use when logging in using PKCE flow - ConnectImxPKCE()
/// </summary>
public async UniTask LogoutPKCE()
/// <param name="hardLogout">If false, the user will not be logged out of Passport in the browser. The default is true.</param>
public async UniTask LogoutPKCE(bool hardLogout = true)
{
await GetPassportImpl().LogoutPKCE();
await GetPassportImpl().LogoutPKCE(hardLogout);
}

/// <summary>
Expand Down
44 changes: 44 additions & 0 deletions src/Packages/Passport/Tests/Runtime/Scripts/PassportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,50 @@ public async Task Login_Logout_Success()
Assert.AreEqual(expectedEvents, authEvents);
}

[Test]
public async Task Login_Soft_Logout_Success()
{
var deviceConnectResponse = new DeviceConnectResponse
{
success = true,
code = CODE,
deviceCode = DEVICE_CODE,
url = URL
};
communicationsManager.AddMockResponse(deviceConnectResponse);
var confirmCodeResponse = new BrowserResponse
{
success = true
};
communicationsManager.AddMockResponse(confirmCodeResponse);
var logoutResponse = new StringResponse
{
success = true,
result = LOGOUT_URL
};
communicationsManager.AddMockResponse(logoutResponse);

// Login
bool success = await passport.Login();
Assert.True(success);

// Logout
await passport.Logout(hardLogout: false);

Assert.AreEqual(1, urlsOpened.Count);
Assert.AreEqual(URL, urlsOpened[0]);
List<PassportAuthEvent> expectedEvents = new List<PassportAuthEvent>{
PassportAuthEvent.LoggingIn,
PassportAuthEvent.LoginOpeningBrowser,
PassportAuthEvent.PendingBrowserLogin,
PassportAuthEvent.LoginSuccess,
PassportAuthEvent.LoggingOut,
PassportAuthEvent.LogoutSuccess
};
Assert.AreEqual(expectedEvents.Count, authEvents.Count);
Assert.AreEqual(expectedEvents, authEvents);
}

[Test]
public async Task Login_InitialiseDeviceCodeAuth_Failed()
{
Expand Down

0 comments on commit 06cea50

Please sign in to comment.