I used the firebase phone auth, after verification, I want to update the logged in users profile detials. So I wrote a cloud function to do that and then call the user.reload() method after the function returned a response. But to my surprise my app gets logged out with this on my console:
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: [firebase_auth/user-token-expired] The user's credential is no longer valid. The user must sign in again.
I did some research and I found out it was a security measure, so I had to re-authenticate the user.
This is what I did:
Future<Response> updateUserprofile({context, name, email}) async{
User _user = _firebaseAuth.currentUser;
Response response = Response(success: false);
Provider.of<AuthenticationProvider>(context, listen: false).loading = true;
try{
HttpsCallableResult functionResponse = await createUser.call({'displayName': name, 'email':email});
response.success = functionResponse.data['success'];
response.message = functionResponse.data['message'];
_user.reload();
AuthCredential credential = AuthCredential(providerId: PhoneAuthProvider.PROVIDER_ID, signInMethod: PhoneAuthProvider.PHONE_SIGN_IN_METHOD);
await user.reauthenticateWithCredential(credential);
Provider.of<AuthenticationProvider>(context, listen: false).loading = false;
return response;
}catch(e){
Provider.of<AuthenticationProvider>(context, listen: false).loading = false;
response.message = e.message;
return response;
}
}
but I still get the same error on my console:
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: [firebase_auth/user-token-expired] The user's credential is no longer valid. The user must sign in again.