How To Make Auto-Login For Your Xamarin.Forms Mobile App

The idea is to allow the user to auto-login after his/her first verified connection. Here, you will find the simple project.

First of all, you need to install this NuGet Package. Or use this command in VS's Package Manager Console.

  1. Install-Package AutoLogin.Mabrouk -Version 1.1.2  
Now, let's code.

In your App.xaml.cs (like hereclass which represents the cross-platform mobile application, you write your test. If the user's information is already saved, then you get directly to the main page and the login page will be ignored,

  1. public App() {  
  2.     InitializeComponent();  
  3.     // If user is saved ; HomePage is opened    
  4.     if (CrossAutoLogin.Current.UserIsSaved) MainPage = new AutoLoginSimple.HomePage();  
  5.     else MainPage = new AutoLoginSimple.MainPage();  
  6. }  
Then, in your connection's button event, save your user's info (Email, Password) like :
  1. private void Connect_clicked(object sender, EventArgs e)  
  2.         {  
  3.            //Do something ...  
  4.              
  5.             // Test if user is already saved   
  6.             if (CrossAutoLogin.Current.UserIsSaved == false)  
  7.             {  
  8.                 CrossAutoLogin.Current.SaveUserInfos(entry_email.Text, entry_password.Text);  
  9.             }  
  10.   
  11.             Navigation.PushModalAsync(new HomePage());  
  12.         }  

Also, if you want to delete user's information, for exemple in DISCONNECT button, you can simply add this line of code -

CrossAutoLogin.Current.DeleteUserInfos();

Example
  1. private void Disconnect_clicked(object sender, EventArgs e)  
  2.  {  
  3.    CrossAutoLogin.Current.DeleteUserInfos();  
  4.    Navigation.PopModalAsync();  
  5.  }  

Source code is available on GitHub.

I hope this tutorial was helpful for you.