0

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.

1 Answer 1

1

You are updating the user's email. This invalidates the Firebase ID token, as described here: https://firebase.google.com/docs/auth/admin/manage-sessions.

It seems to be the expected behaviour as also pointed out here: Firebase Auth - After updating the user's email, Firebase Auth logs out the user. I see that's what you're trying to do as well?

If user.reload() returns that exception, than it still does if you call it before:

AuthCredential credential = AuthCredential(providerId: PhoneAuthProvider.PROVIDER_ID, signInMethod: PhoneAuthProvider.PHONE_SIGN_IN_METHOD);
      await user.reauthenticateWithCredential(credential);

Maybe you should re-authenticate before you reload the user?


One other thing I note is that you use createUser.call. Since you are updating the credentials of an existing user, why not use updateUser (https://firebase.google.com/docs/auth/admin/manage-users#update_a_user)?

Sign up to request clarification or add additional context in comments.

1 Comment

I re-authenticated the user before reloading and it kind of worked for the first time but it doesn't work anymore. I still get the same issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.