Search code examples
c#xamarinxamarin.formsxamarin.androidxamarin.ios

Xamrin.Forms trying to create Email check using Firebase Realtime Database


I'm new to using Xamarin.Forms, I'm currently working on creating a sign up page. I'm trying to create some way to check if an email is already been used when creating an account. I am using firebase realtime database. I manage to get the application to check if the email has been used and display an error, but when the email is valid(not used), when I go to create the account the application doesn't do anything. What am I missing?

I have provided the code

    public Command SignUpCommand
    {
        get
        {
            return new Command(() =>
            {
                var hasSymbols = new Regex(@"[!@#$%^&*()_+=\[{\]};:<>|./?,-]");

                //null or empty field validation, check weather email and password is null or empty
                if (string.IsNullOrEmpty(Email) || string.IsNullOrEmpty(Password))
                {
                    App.Current.MainPage.DisplayAlert("Empty Values", "Please enter Email and Password", "OK");
                }
                else if (!Email.Contains("@"))
                {
                    App.Current.MainPage.DisplayAlert("", "Email Address is Invalid", "OK");
                }
                else if (Password.Length < 8)
                {
                    App.Current.MainPage.DisplayAlert("", "Password is less than 8 characters", "OK");
                }
                else if (!Password.Any(char.IsUpper))
                {
                    App.Current.MainPage.DisplayAlert("", "Password Must Contain at Least 1 Uppcase Letter", "OK");
                }
                else if (!hasSymbols.IsMatch(Password))
                {
                    App.Current.MainPage.DisplayAlert("", "Password should contain At least one special case characters", "OK");
                }
                else if (Password != ConfirmPassword)
                {
                    App.Current.MainPage.DisplayAlert("", "Password must be same as above!", "OK");
                }
                else if (string.IsNullOrEmpty(StudentID))
                {
                    App.Current.MainPage.DisplayAlert("Empty Values", "Please enter Student ID", "OK");
                }
                else if (StudentID.Length < 9)
                {
                    App.Current.MainPage.DisplayAlert("", "Student ID is Incorrect!", "OK");
                }
                else
                {
                    EmailCheck();
                }

            });

        }

    }

    private async void EmailCheck()
    {

        //call GetUser function which we define in Firebase helper class
        var user = await FirebaseHelper.GetUser(Email);
        //firebase return null valuse if user data not found in database
        if (user != null)
            if (Email == user.Email)
            {
                await App.Current.MainPage.DisplayAlert("Email Already Used", "", "Ok");
            }
            else
                SignUp();
    }

    private async void SignUp()
    {
        //call AddUser function which we define in Firebase helper class
        var user = await FirebaseHelper.AddUser(Email, Password, StudentID, FirstName, LastName, CarMake, CarModel,
                                                CarYear, CarColor, LicenseNumber);
        //AddUser return true if data insert successfuly 
        if (user)
        {
            await App.Current.MainPage.DisplayAlert("SignUp Success", "", "Ok");
            //Navigate to Wellcom page after successfuly SignUp
            //pass user email to welcom page
            await App.Current.MainPage.Navigation.PushAsync(new LoginPage());
        }
        else
            await App.Current.MainPage.DisplayAlert("Error", "SignUp Fail", "OK");
    }
}

}


Solution

  • According to your description, you want to create project, Register account using Email account, and sign up to navigate another content page.

    But I find your code is a bit messy, I do one simple sample that you can take a look:

     <StackLayout>
        <!--  Place new controls here  -->
        <Entry x:Name="txtemail" Placeholder="Email" />
        <Entry x:Name="txtpassword" Placeholder="Enter Password" />
        <Entry x:Name="txtstudentId" Placeholder="Enter Student Id" />
        <StackLayout HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
            <Button
                x:Name="btnSign"
                Clicked="BtnSign_Clicked"
                Text="Sign up"
                WidthRequest="200" />
            <Button
                x:Name="btnRegister"
                Clicked="BtnRegister_Clicked"
                Text="Retrive"
                WidthRequest="200" />
        </StackLayout>
    </StackLayout>
    
     public partial class MainPage : ContentPage
    {
        FirebaseHelper firebaseHelper = new FirebaseHelper();
        public MainPage()
        {
            InitializeComponent();
        }
    
        private async void BtnSign_Clicked(object sender, EventArgs e)
        {
            var person = await firebaseHelper.GetPerson(txtemail.Text);
            if (person != null)
            {
                if(person.Email==txtpassword.Text)
                {
                    await Navigation.PushAsync(new Page1());
                }
                else
                {
                    await DisplayAlert("Fail", "Password is not correct!", "OK");
                }              
            }
            else
            {
                await DisplayAlert("Fail", "No email Available!", "OK");
            }
        }
    
        private async void BtnRegister_Clicked(object sender, EventArgs e)
        {
            if(await RegisterAccount())
            {
                await firebaseHelper.AddPerson(txtemail.Text, txtpassword.Text, txtstudentId.Text);
                txtemail.Text = string.Empty;
                txtpassword.Text = string.Empty;
                txtstudentId.Text = string.Empty;
                await DisplayAlert("Success", "Person Added Successfully", "OK");
            }
            else
            {
                await DisplayAlert("Fail", "Person Added Fail", "OK");
            }
    
    
        }
    
        private async Task<bool> RegisterAccount()
        {
            var hasSymbols = new Regex(@"[!@#$%^&*()_+=\[{\]};:<>|./?,-]");
    
            //null or empty field validation, check weather email and password is null or empty
            if (string.IsNullOrEmpty(txtemail.Text) || string.IsNullOrEmpty(txtpassword.Text))
            {
              await  DisplayAlert("Empty Values", "Please enter Email and Password", "OK");
    
                return false;
            }
            else if (!txtemail.Text.Contains("@"))
            {
                await DisplayAlert("", "Email Address is Invalid", "OK");
                return false;
            }
            else if (txtpassword.Text.Length < 8)
            {
                await DisplayAlert("", "Password is less than 8 characters", "OK");
                return false;
            }
            else if (!txtpassword.Text.Any(char.IsUpper))
            {
                await DisplayAlert("", "Password Must Contain at Least 1 Uppcase Letter", "OK");
                return false;
            }
            else if (!hasSymbols.IsMatch(txtpassword.Text))
            {
                await DisplayAlert("", "Password should contain At least one special case characters", "OK");
                return false;
            }
    
            else if (string.IsNullOrEmpty(txtstudentId.Text))
            {
                await DisplayAlert("Empty Values", "Please enter Student ID", "OK");
                return false;
            }
    
            else
            {
                var person = await firebaseHelper.GetPerson(txtemail.Text);
                if (person != null)
                {
                    await DisplayAlert("fail", "Email already exist", "OK");
                    return false;
    
                }
            }
    
            return true;
    
        }
    }
    
    public class Register
    {
        public string Email { get; set; }
        public string Password { get; set; }
        public string StudentId { get; set; }
    }
    
     public class FirebaseHelper
    {
        FirebaseClient firebase = new FirebaseClient("https://testapp1-ebb27-6e5ba.firebaseio.com/");
    
        public async Task<List<Register>> GetAllPersons()
        {
    
            return (await firebase
              .Child("Persons")
              .OnceAsync<Register>()).Select(item => new Register
              {
                  Email = item.Object.Email,
                  Password = item.Object.Password,
                  StudentId=item.Object.StudentId
    
              }).ToList();
        }
    
        public async Task AddPerson(string email, string password, string studentid)
        {
    
            await firebase
              .Child("Persons")
              .PostAsync(new Register() { Email = email, Password = password, StudentId = studentid });
    
        }
    
        public async Task<Register> GetPerson(string email)
        {
            var allPersons = await GetAllPersons();
            await firebase
              .Child("Persons")
              .OnceAsync<Register>();
            return allPersons.Where(a => a.Email == email).FirstOrDefault();
        }
    
    
    }
    

    This is my sample at github:

    https://github.com/CherryBu/RegisterApp

    enter image description here