Search code examples
c#firebasexamarin.formsfirebase-authenticationjson.net

FirebaseAuthentication.net package in Xamarin Forms - get current user UID


In my Xamarin.Forms project, I saw that the FirebaseAuthentication.Net package can be used for the cross-platform project. How would I get the current user's UID with this? I have figured out how to get the current user's email by saving the firebase refresh token and using NewtonSoft.JSON, but am not sure how to do this with the id. I want to be able to do this so that I can store the user's data in the database under their uid and retrieve it that way.

Here's what I have so far for getting the email address of the current user:

var authProvider = new FirebaseAuthProvider(new FirebaseConfig(WebAPIKey));

var savedfirebaseauth = JsonConvert.DeserializeObject<Firebase.Auth.FirebaseAuth>(Preferences.Get("FirebaseRefreshToken", ""));
var RefreshedContent = await authProvider.RefreshAuthAsync(savedfirebaseauth);
Preferences.Set("FirebaseRefreshToken", JsonConvert.SerializeObject(RefreshedContent));
string UsersEmailToDisplay = savedfirebaseauth.User.Email;

What I am storing as "FirebaseRefreshToken" is the following.

var auth = await authProvider.CreateUserWithEmailAndPasswordAsync(Email.Text, Password.Text);
var content = await auth.GetFreshAuthAsync();
var serializedcontent = JsonConvert.SerializeObject(content);

Please let me know if any more clarification of my question is needed.


Solution

  • I think you want to get the UserId which unique to the Firebase project.

    You could try to call its LocalId after you sign in.

    var authProvider = new FirebaseAuthProvider(new FirebaseConfig(""));
    var auth = await authProvider.SignInWithOAuthAsync(authType, accessToken);
    var uid = auth.User.LocalId;