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

Web auth receive token, but doesn't return to app #160

Open
stryder123451 opened this issue Nov 27, 2023 · 2 comments
Open

Web auth receive token, but doesn't return to app #160

stryder123451 opened this issue Nov 27, 2023 · 2 comments

Comments

@stryder123451
Copy link

After successful authorization , i must close browser by myself. On iOS it works normal, and app open by itself

@kennydead
Copy link

Might not be the same issue as we had but in our case on android, the issue was in the android manifest. Where the MainActivity is defined, there is a parameter called "android:launchMode". for us, it didn't redirect back until we set that to "singleTask"

@andregurgel
Copy link

I managed to solve it in a different way:

  1. I kept the 'android:launchMode="singleTop"', as it was set when the app was created.
  2. I ended up removing the 'android:taskAffinity=""' property because I found in another source that it's recommended to remove this property to fix the issue.
  3. I changed the app ID in the path 'android/app/src/build.gradle', specifically the 'applicationId' property in the 'defaultConfig'. (This ID is a unique identifier for the app; you can set it to whatever you like.)
  4. My AndroidManifest now looks as follows:
<activity
    android:name="com.linusu.flutter_web_auth.CallbackActivity" 
    android:exported="true">
    <intent-filter android:label="flutter_web_auth">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:scheme="com.codecentauri.nps"
            android:host="oauth"
            android:path="/callback" />
    </intent-filter>
</activity>

Note that the value of the android:scheme property must match the ID you set in the build.gradle file.

  1. My method that opens the browser to perform the login is now as follows:
Future<void> authenticate() async {
    final grant = OAuth2.AuthorizationCodeGrant(
      clientId,
      authorizationEndpoint,
      tokenEndpoint,
      secret: clientSecret,
    );

    final authorizationUrl = grant.getAuthorizationUrl(
      Uri.parse(redirectUrl),
      scopes: scopes,
    );

    try {
      logger.i('Opening browser to authenticate the user.');

      final result = await FlutterWebAuth.authenticate(
        url: authorizationUrl.toString(),
        callbackUrlScheme: 'com.codecentauri.nps',
      );

      final code = Uri.parse(result).queryParameters['code'];
      if (code == null) {
        throw Exception('Authorization code not found.');
      }

      final client = await grant.handleAuthorizationResponse({'code': code});
      await _saveCredentials(client.credentials);
      _client = client;
    } on OAuth2.AuthorizationException catch (e) {
      logger.e('Error during authentication: $e');
    } on Exception catch (e) {
      logger.e('Unknown error during authentication: $e');
    }
  }

Ensure the callbackUrlScheme matches the ID defined in build.gradle.

  1. Finally, the value of my redirectUrl variable is 'com.codecentauri.nps://oauth/callback'.

Extra:
If you encounter issues when testing with the .apk in release mode, remember to add the following tag in the AndroidManifest:

<uses-permission android:name="android.permission.INTERNET"/>

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

No branches or pull requests

3 participants