supabase / supabase-flutter

Flutter integration for Supabase. This package makes it simple for developers to build secure and scalable products.
https://supabase.com/
MIT License
656 stars 154 forks source link

AuthChangeEvent does not trigger after loggining in using signInWithOAuth (`Google` or `Apple`). #935

Closed Vedsaga closed 3 weeks ago

Vedsaga commented 1 month ago

Describe the bug

We login via OAuth, was expecting that when redirected back to the app, AuthChangeEvent should trigger, which does not trigger. And even checking the currentSession manually is null.

Have went through the discussion, around https://github.com/supabase/supabase-flutter/issues/819. We don't even receive any email-confirmation ( assuming for Google/Apple) there won't be.

below is our OAuth method,

  Future<Either<Failure, bool>> connectWithOAuth(
    AuthType authRequestType,
    UserflowEnum userFlow,
  ) async {
    try {
      final oAuthProvider = switch (authRequestType) {
        AuthType.apple => OAuthProvider.apple,
        AuthType.facebook => OAuthProvider.facebook,
        AuthType.google => OAuthProvider.google,
        AuthType.email => throw const AuthException(
            'Invalid auth type.',
          ),
      };
      final response = await Instance.supabase.client.auth.signInWithOAuth(
        oAuthProvider,
        redirectTo: EnvKeyEnum.getEnvValue(EnvKeyEnum.redirectPostOAuthSuccess),
      );
      await Instance.oneTapLoginTableQuery.insertOneTapLoginRow(
        authType: authRequestType,
      );
      return Right(response);
    } catch (error) {
      return Left(Failure.fromErrorObject(error, userFlow: userFlow));
    }
  }

As we listen for the redirect URL ( as we are using app_links), so when we get redirected the app-links does catch the uri, below is how we listen,

  void _initializeSubscriptions() {
    _internetSubscription =
        Instance.internetBloc.stream.listen(_handleInternetStateChange);

    _authSubscription =
        Instance.authBloc.stream.listen(_handleAuthorizationStateChange);

    _deepLinkUriSubscription =
        Instance.appLinks.uriLinkStream.listen(_determineRedirectUri);
  }

  void _determineRedirectUri(Uri uri) {
    final currentSession = Instance.supabase.client.auth.currentSession;
    print('Incoming deeplink: ${uri.path}');
    // query params
    print('Query params: ${uri.queryParameters}');
  }

and we can see the after successful redirect, in the console, we get following print

I/flutter ( 6151): Incoming deeplink: /oauth
I/flutter ( 6151): Query params: {code: c5167357-f8bb-49fb-b792-9464ca0314f1}

The logs, seems to confirm that oauth succeeded, just that AuthChangeEvent is not triggering... Nither receive any email for confirmation, even though we disabled the required email verification ( so this should be concern for now)

Expected behavior

It is expected that AuthChangeEvent should have trigged.

Version (please complete the following information):

On Windows

 dart pub deps | findstr "supabase gotrue postgrest storage_client realtime_client functions_client"
Γö£ΓöÇΓöÇ supabase_flutter 2.5.3
Γöé   Γö£ΓöÇΓöÇ supabase 2.1.3
Γöé   Γöé   Γö£ΓöÇΓöÇ functions_client 2.2.0
Γöé   Γöé   Γö£ΓöÇΓöÇ gotrue 2.7.0
Γöé   Γöé   Γö£ΓöÇΓöÇ postgrest 2.1.2
Γöé   Γöé   Γö£ΓöÇΓöÇ realtime_client 2.0.4
Γöé   Γöé   Γö£ΓöÇΓöÇ storage_client 2.0.1

Additional context

Email which is used while OAuth, that email did existed earlier and account was created via email + password flow.

Also, tried AuthFlowType.implicit same behaviour

Supabase.initialize(
        url: EnvKeyEnum.getEnvValue(EnvKeyEnum.supbaseUrl),
        anonKey: EnvKeyEnum.getEnvValue(EnvKeyEnum.supabaseAnonKey),
          authOptions: const FlutterAuthClientOptions(
          authFlowType: AuthFlowType.implicit,
        ),
      )
Vedsaga commented 4 weeks ago

Can't seems to change the title, but can say that

AuthChangeEvent does not trigger is a bug

But the title by mistake is AuthChangeEvent does trigger...

Hopeful that there is some work around... As we plan to release the app by 29th

cc: @dshukertjr

dshukertjr commented 3 weeks ago

@Vedsaga This is happening probably because you are listening to deep links on your own. supabase_flutter handles deep links automatically without you having to do anything, and you setting up your own deep link listener using app-link interferes with it.

If you must listen to deep links using app-links, you can explicitly call supabase.auth.getSessionFromUrl() method when you receive a deep link.