when i try to login in android device with apple in a flutter app, it fails and shows “Unable to process request due to missing initial state. This may happen if browser sessionStorage is inaccessible or accidentally cleared. Some specific scenarios are - 1) Using IDP-Initiated SAML SSO. 2) Using signInWithRedirect in a storage-partitioned browser environment.” but works with apple devices. Here is my implemented code below:
Future<UserModel> signInWithApple() async {
final AuthorizationCredentialAppleID credential =
await SignInWithApple.getAppleIDCredential(
scopes: [
AppleIDAuthorizationScopes.email,
AppleIDAuthorizationScopes.fullName
],
webAuthenticationOptions: WebAuthenticationOptions(
clientId: '...',
redirectUri: Uri.parse(
'...',
),
),
);
// Authenticate with Firebase using Apple credentials
final OAuthCredential oAuthCredential =
OAuthProvider("apple.com").credential(
idToken: credential.identityToken,
accessToken: credential.authorizationCode,
);
final UserCredential firebaseUserCredential =
await
FirebaseAuth.instance.signInWithCredential(oAuthCredential);
// Get the ID token from Firebase
final idToken = await
firebaseUserCredential.user?.getIdToken();
final response = await http.post(
Uri.parse('$baseUrl/oauthlogin'),
headers: {
'Content-Type': 'application/json',
},
body: jsonEncode({'idToken': idToken}),
);
if (response.statusCode == 200) {
final data = json.decode(response.body);
final token = data['token'];
if (token != null) {
await _preferenceManager.setUserToken(token);
}
return UserModel.fromJson(data);
} else {
throw Exception('Anmeldung bei Apple fehlgeschlagen');
}
}