aws-amplify / amplify-flutter

A declarative library with an easy-to-use interface for building Flutter applications on AWS.
https://docs.amplify.aws
Apache License 2.0
1.3k stars 239 forks source link

feature request: Emit info from signInWithWebUI through Auth Hub #4986

Open sourabhguptazeil opened 1 month ago

sourabhguptazeil commented 1 month ago

Description

When I execute the following code on the web, after redirect flutter app restarts hence this method doesn't wait and return anything

Future<SignInResult?> signInWithGoogle() async {
    try {
      final result =
          await Amplify.Auth.signInWithWebUI(provider: AuthProvider.google);
      safePrint('Is signed in successful: ${result.isSignedIn}');
      return result;
    } on UserCancelledException {
      return null;
    } on AmplifyException catch (e) {
      return null;
    }
  }

Categories

Steps to Reproduce

  1. Start social login on web
  2. Redirect URI is successfully called
  3. Instead of resuming the previous flutter instance and continue with method execution, app restarts hence method doesn't return the result.

Screenshots

No response

Platforms

Flutter Version

3.22.1

Amplify Flutter Version

2.1.0

Deployment Method

Custom Pipeline

Schema

No response

Jordan-Nelson commented 1 month ago

@sourabhguptazeil This is currently expected behavior. Since the browser redirects to the third party provider, the previous app state is lost.

When the application reloads you can call fetchAuthSession() to see if there is an authenticated user. Most likely you would want to do this when the application first loads anyway. If there is an error I believe the url will contain an error query param.

We could consider adding support for authenticating in a separate window so that the application maintains state, or making it easier to check for success/error after the app reloads.

What is the use case you are trying to achieve?

Jordan-Nelson commented 1 month ago

Thanks @tc-sgupta - We have marked this as a feature request to maintain application state. I believe you could pull the error from the query param in the URI in the meantime.

sourabhguptazeil commented 1 month ago

When we receive an error, I would like to show an error. Since app state is lost, app screen changes too many times due to reloading everything.

It would be really good to launch social login in a separate window and it will help to avoid app restart and app state won't be lost.

Jordan-Nelson commented 1 month ago

@sourabhguptazeil Amplify JS emits Auth Hub events after the Hosted UI redirect for success/error/custom state. These allow customers to listen for this info when the app launches and perform some action (ex: show an error message, route to s specific screen) when the application reloads.

Amplify Flutter does not provide these hub events. If we were to add them would this satisfy your use case? It is likely we would pursue the same approach as Amplify JS to keep parity across libraries, but let me know if this doesn't work for your use case.

I did want to call out again that the error info should be available in a query param when the application reloads. As workaround you could parse the info from the URI. I recognize this is not ideal though.

Here are the Amplify JS docs: https://docs.amplify.aws/react/build-a-backend/auth/concepts/external-identity-providers/#required-for-multi-page-applications-complete-external-sign-in-after-redirect

sourabhguptazeil commented 1 month ago

@Jordan-Nelson I agree we can have consistent logic on flutter as well. I'm thinking if flutter app state will be lost after navigating to third party provider, how Auth Hub events will be fired

Jordan-Nelson commented 1 month ago

@sourabhguptazeil Amplify Flutter would parse the info from the URI and then emit the events using that info.

sourabhguptazeil commented 1 month ago

@Jordan-Nelson Sure thing, we can try Auth hub events then.

Jordan-Nelson commented 1 month ago

Thanks for the confirmation @sourabhguptazeil