Web auth receive token, but doesn't return to app
After successful authorization , i must close browser by myself. On iOS it works normal, and app open by itself
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"
I managed to solve it in a different way:
- I kept the
'android:launchMode="singleTop"', as it was set when the app was created. - 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. - 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.) - My
AndroidManifestnow 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.
- 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.
- Finally, the value of my
redirectUrlvariable 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"/>