Ashley
Ashley

Reputation: 1

Unable to Create a New User using Firebase (Xamarin.Android)

Whenever I try to register a user through the app, it always fails even if the all the requirements are met. I included in the manifest but that didn't fix anything. The email/password sign-in method is also enabled.

I'm really lost as to what to do because this is my first time using xamarin and there seems to be more help when it comes to xamarin forms vs xamarin android.

    public class registerActivity : AppCompatActivity
    {
        TextInputLayout fullNameText;
        TextInputLayout phoneText;
        TextInputLayout emailtext;
        TextInputLayout passwordText;
        Button registerButton;

        FirebaseAuth firebaseAuth;
        FirebaseDatabase userCredentials;

        taskCompletetionListener taskCompletetionListener = new taskCompletetionListener();
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your application here
            SetContentView(Resource.Layout.registerPage);

            Typeface tf = Typeface.CreateFromAsset(Assets, "Tw_Cen_MT.TTF");

            var signUpTxt = FindViewById<TextView>(Resource.Id.signUpTxt);
            signUpTxt.SetTypeface(tf, TypefaceStyle.Bold);

            var signUpLink = FindViewById<TextView>(Resource.Id.signInLink);
            signUpLink.SetTypeface(tf, TypefaceStyle.Bold);

            var alreadyMemberTxt = FindViewById<TextView>(Resource.Id.haveAccTxt);
            signUpTxt.SetTypeface(tf, TypefaceStyle.Bold);

            Button signUpBtn = (Button)FindViewById(Resource.Id.registerBtn);
            signUpBtn.SetTypeface(tf, TypefaceStyle.Bold);

            InitializedFirebase();
            firebaseAuth = FirebaseAuth.Instance;
            ConnectControl();
        }

        void InitializedFirebase()
        {
            var app = FirebaseApp.InitializeApp(this);

            if (app == null)
            {
                var options = new FirebaseOptions.Builder()
                    .SetApplicationId("semoto-4b53b")
                    .SetApiKey("AIzaSyBiGjJQwDewsyWxviBjIimw7pQBHkbHkiA")
                    .SetDatabaseUrl("https://semoto-4b53b.firebaseio.com")
                    .SetStorageBucket("semoto-4b53b.appspot.com")
                    .Build();

                app = FirebaseApp.InitializeApp(this, options);
                userCredentials = FirebaseDatabase.GetInstance(app);
            }

            else
            {
                userCredentials = FirebaseDatabase.GetInstance(app);
            }
        }

        void ConnectControl()
        {
            fullNameText = (TextInputLayout)FindViewById(Resource.Id.nameField);
            emailtext = (TextInputLayout)FindViewById(Resource.Id.emailField);
            phoneText = (TextInputLayout)FindViewById(Resource.Id.phoneNumField);
            passwordText = (TextInputLayout)FindViewById(Resource.Id.passwordField);
            registerButton = (Button)FindViewById(Resource.Id.registerBtn);

            registerButton.Click += RegisterButton_Click;
        }

        private void RegisterButton_Click(object sender, EventArgs e)
        {
            string fullName, phoneNum, email, password;
            fullName = fullNameText.EditText.Text;
            phoneNum = phoneText.EditText.Text;
            email = emailtext.EditText.Text;
            password = passwordText.EditText.Text;

            if(fullName.Length <= 3)
            {
                fullNameText.ErrorEnabled = true;
                fullNameText.Error = "Please enter a valid name";
                return;
            }
            else if (!email.Contains("@"))
            {
                emailtext.ErrorEnabled = true;
                emailtext.Error = "Please enter a valid email";
                return;
            }
            else if(phoneNum.Length < 9)
            {
                phoneText.ErrorEnabled = true;
                phoneText.Error = "Please enter a valid phone number";
                return;
            }
            else if (password.Length < 8)
            {
                passwordText.ErrorEnabled = true;
                passwordText.Error = "Password should be at least 8 characters long";
                return;
            }

            registerUser(fullName, phoneNum, email, password);
        }

        void registerUser(string name, string phone, string email, string password)
        {
            taskCompletetionListener.success += TaskCompletetionListener_success;
            taskCompletetionListener.failure += TaskCompletetionListener_failure;
            firebaseAuth.CreateUserWithEmailAndPassword(email, password)
                .AddOnSuccessListener(this, taskCompletetionListener)
                .AddOnFailureListener(this, taskCompletetionListener);

        }

        private void TaskCompletetionListener_failure(object sender, EventArgs e)
        {
            Android.Support.V7.App.AlertDialog.Builder alert = new Android.Support.V7.App.AlertDialog.Builder(this);
            alert.SetTitle("Registration Failed!");
            alert.SetMessage("Ensure that you meet all of the requirements. ");
            alert.SetNeutralButton("OK", delegate
            {
                alert.Dispose();
            });
            alert.Show();
        }
        //reishinishite kudasai
        private void TaskCompletetionListener_success(object sender, EventArgs e)
        {
            Android.Support.V7.App.AlertDialog.Builder alert = new Android.Support.V7.App.AlertDialog.Builder(this);
            alert.SetTitle("Registration Successful");
            alert.SetMessage("Welcome!");
            alert.SetNeutralButton("OK", delegate
            {
                alert.Dispose();
            });
            alert.Show();
        }
    }
}

I made a class for the task completion listener

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.Gms.Tasks;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace Semoto.eventListeners
{
    public class taskCompletetionListener : Java.Lang.Object, IOnSuccessListener, IOnFailureListener
    {
        public event EventHandler success;
        public event EventHandler failure;
        public void OnFailure(Java.Lang.Exception e)
        {
            failure?.Invoke(this, new EventArgs());
        }

        public void OnSuccess(Java.Lang.Object result)
        {
            success?.Invoke(this, new EventArgs());
        }
    }
}

Upvotes: 0

Views: 90

Answers (1)

Leo Zhu
Leo Zhu

Reputation: 14956

This is not an answer,just to make it easier for you to troubleshoot.

First step you need to know what caused the failure.

Change your OnFailure listener like:

 public void OnFailure(Java.Lang.Exception e)
  {
     failure?.Invoke(e, new EventArgs());
  }

then in your activity,print the erro message,here use your Dialog to display:

 private void TaskCompletetionListener_failure(object sender, EventArgs e)
    {
        Android.Support.V7.App.AlertDialog.Builder alert = new Android.Support.V7.App.AlertDialog.Builder(this);
        alert.SetTitle("Registration Failed!");
        alert.SetMessage(((Java.Lang.Exception)sender).Message);
        alert.SetNeutralButton("OK", delegate
        {
            alert.Dispose();
        });
        alert.Show();
    }

then you could show your error message that we can help you find a solution faster.(Ps:I use your code to test with my account,it works well).

Upvotes: 1

Related Questions