Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Ho to save user with auth UID as ID using Firebase Auth and Realtime Database

I implemented Firebase Realtime Database and Auth. When user registers it is saved in Authentication with UID. I need to save the user in Realtime Database using the UID as its ID.

I'm working cross platform so I have myproject and my project.Android. In this last one I get NuGet Package Firebase Auth, so I created AuthDroid.cs like that

public class AuthDroid : LogIn
{
public bool IsSignIn()
{
var user = Firebase.Auth.FirebaseAuth.Instance.CurrentUser;
return user != null;
}

    public string CurrentUser()
    {
        var user = Firebase.Auth.FirebaseAuth.Instance.CurrentUser;
        var uid = user.Uid;
        return uid.ToString();
    }

public async Task SignUpWithEmailAndPassword(string email, string password)
{
try
{
var newUser = await Firebase.Auth.FirebaseAuth.Instance.CreateUserWithEmailAndPasswordAsync(email, password);
var token = await newUser.User.GetIdTokenAsync(false);
return token.Token;
}

        catch (FirebaseAuthInvalidCredentialsException e)
        {
            e.PrintStackTrace();
            return string.Empty;
        }
    }

Now, my registration page xaml.cs is like this

String ID;
LogIn auth;
public Registrazione()
{
InitializeComponent();
auth = DependencyService.Get();
//var userID = auth.CurrentUser();
}

    async void RegistraClicked(object sender, EventArgs e)
    {
        FirebaseClient firebaseClient = new FirebaseClient("https://xbookish-3cb78.firebaseio.com/");
        var user = auth.SignUpWithEmailAndPassword(EmailInput.Text, PasswordInput.Text);
        //var userID = auth.CurrentUser();
        //ID = userID.ToString();

        if (user != null)
        {
            Application.Current.MainPage = new FlyoutMainMenu();

            await firebaseClient
                   .Child("Utenti")
                   .Child(auth.CurrentUser())
                   .PostAsync(new Utente() { Nome = NomeInput.Text, Email = EmailInput.Text, Password = PasswordInput.Text });

        } else
        {
            await DisplayAlert("");
        }

    }

Some parts are commented because I tried different ways but the ending is the same: the user registers in auth but he doesn't in realtime database and app throws a NullReferenceException while running, probably because auth in auth.CurrentUser() is null, but if I use user.CurrentUser() gives me error, what can I do?

Answers

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    edited November 2020

    app throws a NullReferenceException while running,

    Which command caused the error? Try adding a breakpoint to test to get the line code.

    the user registers in auth but he doesn't in realtime database

    And is the code in the 'if' statement executed? Make sure the user is not null.

    if (user != null)
    {
        ...
    }
    

    You could google with the keyword as Firebase Auth on Xamarin.Forms and **
    Xamarin.Forms - Working With Firebase Realtime Database CRUD Operations** to check related tutorials.

  • florielfloriel Member

    @Jarvan said:

    app throws a NullReferenceException while running,

    Which command caused the error? Try adding a breakpoint to test to get the line code.

    the user registers in auth but he doesn't in realtime database

    And is the code in the 'if' statement executed? Make sure the user is not null.

    if (user != null)
    {
        ...
    }
    

    You could google with the keyword as Firebase Auth on Xamarin.Forms and **
    Xamarin.Forms - Working With Firebase Realtime Database CRUD Operations** to check related tutorials.

    The if is executed and the error is caused by auth.CurrentUser(), no matter how i put it and where, it gives me error.

  • YelinzhYelinzh Member, Xamarin Team Xamurai

    Was the user created sucessfully? Try add break points at the two following line code to check if the 'users' are valid.

    public string CurrentUser()
    {
        var user = Firebase.Auth.FirebaseAuth.Instance.CurrentUser;
        ...
    }
    public async Task SignUpWithEmailAndPassword(string email, string password)
    {
        try
        {
            var newUser = await Firebase.Auth.FirebaseAuth.Instance.CreateUserWithEmailAndPasswordAsync(email, password);
            ...
        }
        ...
    }
    
Sign In or Register to comment.