Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for app state argument #485

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

navaronbracke
Copy link

@navaronbracke navaronbracke commented Nov 19, 2024

Fixes #486

  • All new/changed/fixed functionality is covered by tests (or N/A)
  • I have added documentation for all new/changed functionality (or N/A)

📋 Changes

This PR adds support for the appState parameter to loginWithRedirect() for the web.
See https://github.com/auth0/auth0-spa-js/blob/f2e566849efa398ca599daf9ebdfbbd62fcb1894/src/global.ts#L298

There is prior art in auth0/auth0-angular#168

🎯 Testing

Added several unit tests. This can be tested end-to-end by setting up a login with redirect, that uses the app state argument as parameter, and then validating that the app state is returned when the user is redirected back.
I did verify end-to-end using an internal application that uses the SDK integration.

To run the unit tests locally, use flutter test test/web/auth0_flutter_web_test.dart --platform=chrome

@navaronbracke navaronbracke requested a review from a team as a code owner November 19, 2024 16:16
@@ -21,6 +21,7 @@ dependency_overrides:

dev_dependencies:
build_runner: ^2.1.8
collection: ^1.18.0
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the MapEquality in the test, hence a dev dependency

@navaronbracke navaronbracke marked this pull request as draft November 20, 2024 09:08
@navaronbracke
Copy link
Author

It seems I still missed a step, as the app state is not passed to the redirect url when actually coming back to the app. Once that is fixed, this is ready for review.

@@ -20,8 +20,8 @@ class Auth0FlutterWebClientProxy {
[final GetTokenSilentlyOptions? options]) =>
promiseToFuture(client.getTokenSilently(options));

Future<void> handleRedirectCallback() =>
promiseToFuture(client.handleRedirectCallback());
Future<RedirectLoginResult> handleRedirectCallback([final String? url]) =>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value and parameters didn't match what is defined in the JS SDK.

For the fix, I need the new return value, but the url is currently unused.
Adding support for custom URL's could be done later, I guess.

final PopupLoginOptions? options,
final PopupConfigOptions? config,
]);
external Future<RedirectLoginResult> handleRedirectCallback([
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The app state is defined in the RedirectLoginResult, so we need it here too.

/// Thus clearing this object is not needed,
/// as the actual state is managed across reloads,
/// using the transaction manager.
Object? _appState;
Copy link
Author

@navaronbracke navaronbracke Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property & getter are new. We provide these now, so that:
a) users can retrieve the app state
b) we do not need to change the API for initialize/onLoad (the app state is web only anyway? Or so I think?)

In the future, we might want to move the redirect state to the result of onLoad() / initialize() directly. That would make the API cleaner, but that does involve a breaking change, so the plugin would have to do this in version 2.0.0

@@ -31,4 +31,22 @@ class JsInteropUtils {

return obj;
}

// TODO: replace with `dartify` from `dart:js_interop_unsafe` when migrating to WASM
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added these here, to make the migration to WASM easier. Then we only need to change these two, instead of all callsites

@@ -72,13 +74,75 @@ void main() {

test('handleRedirectCallback is called on load when auth params exist in URL',
() async {
final interop.RedirectLoginResult mockRedirectResult =
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was updated. Now it also mocks handleRedirectCallback

verifyNever(mockClientProxy.checkSession());
});

test('handleRedirectCallback captures appState that was passed', () async {
Copy link
Author

@navaronbracke navaronbracke Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New test to verify behavior of the app state

expect(eq.equals(capturedAppState, appState), isTrue);
});

test('appState getter returns null when accessed more than once', () async {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New test to verify behavior of the app state

@@ -160,6 +224,33 @@ void main() {
expect(params.max_age, null);
});

test('loginWithRedirect supports appState parameter', () async {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New test to verify behavior of the app state

@navaronbracke navaronbracke marked this pull request as ready for review November 21, 2024 11:07
@navaronbracke
Copy link
Author

Failing test was fixed, so this is ready for review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The appState parameter is missing for login with redirect on the web
1 participant