Username Restriction in firebase using xamarin form

MSXAM 41 Reputation points
2022-12-06T12:07:15.457+00:00

My problem is how to avoid duplication of username in xamarin using firebase.

My expected output is upon tapping the username field, if in the database, "usernametest123" is already in there and a new customer upon his/her creating account to my app, and enter a username "usernametest123", a toast will appear like "Username is already exist". I tried this way; my code behind.

 async public void CreateAccount_Clicked(System.Object sender, System.EventArgs e)  
{  
  
//thats how I declare some variable up to my 11 input fields  
 string FName = textfield_Firstname.Text;  
 if (string.IsNullOrEmpty(FName )){await this.DisplayToastAsync("Please Enter your first name", 1500);}  
//up to 11 input fields also  
  
                CUSTOMER customer = new CUSTOMER();  
                customer.CustomerFName = FName;  
                var Savedata = await customerRepo.Save(customer);  
  
}  
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,291 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,021 Reputation points Microsoft Vendor
    2022-12-07T06:11:22.85+00:00

    Hello,

    You could use the Entry's TextChanged event to implement this feature to alert the user if this username exists.

    If you already have a method to verify usernames from FireBase, which is an asynchronous method called VerifyIfUserNameExists, you could refer to the following code:

       private async void userName_TextChanged(object sender, TextChangedEventArgs e)  
               {  
                   if(e.NewTextValue != string.Empty)  //Detection of presence begins when the user starts typing  
                   {  
                       if(await VerifyIfUserNameExists(e.NewTextValue))  
                       {  
                           AlertLabel.IsVisible = true; // Use a label to alert.  
                           AlertLabel.Text = "This username has been existed!";  
                       }else  
                       {  
                           AlertLabel.IsVisible = false;  
                           AlertLabel.Text = "";  
                       }  
                   }else  
                   {  
                       AlertLabel.IsVisible = false;  
                       AlertLabel.Text = "";  
                   }  
               }  
         
       <Entry x:Name="userName" TextChanged="userName_TextChanged"/>  
       <Label x:Name="AlertLabel" IsVisible="false" TextColor="Red"/>  
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.