public App() { InitializeComponent(); //Get token details from sqlite db for validation //If DB is null, will redirect to Login Screen (FastBatPage) //Other wise validate the token expiry, if token is valid will redirect to events list screen var token = App.Database.GetTokenDetails(); if (token == null) { MainPage = new FastBatPage(); } else { //Validate token expiry details DateTime issueDate = Convert.ToDateTime(token.issued); DateTime currentDate = Convert.ToDateTime(DateTime.UtcNow.ToString("R")); double totlaSeconds = (currentDate - issueDate).TotalSeconds; if (totlaSeconds > token.expires_in) { //Delete the existing token App.Database.DeleteToken(); //Delete the events App.Database.DeleteEvents(); //Redirect MainPage = new FastBatPage(); } else { MainPage = new EventsView(token.access_token); } } }
/// <summary> /// Logins the process. /// </summary> /// <returns>The process.</returns> async Task LoginProcess() { await Task.Delay(1000); if (viewModel.CanLogin()) { try { //Checking internet connection bool internetConnection = await Authentication.CheckForInternetConnection(); //If internet is working properly, will verify the login details using api if (internetConnection) { //Get the login result details based on user details var result = await Authentication.GetToken(viewModel.Email, viewModel.Password); string strResult = result.Replace(".issued", "issued"); result = strResult.Replace(".expires", "expires"); var tokenResult = (TokenModel)JsonConvert.DeserializeObject(result, typeof(TokenModel)); if (tokenResult.error == null && tokenResult.error_description == null) { //Save token details in sqlite database App.Database.SaveTokenDetails(tokenResult); //Redirect to Event Page var homePage = new EventsView(tokenResult.access_token); acInLoading.IsRunning = false; await this.Navigation.PushModalAsync(homePage); } else { //Displaying message for login failure acInLoading.IsRunning = false; await DisplayAlert(string.Empty, tokenResult.error_description, "OK"); } } else { await DisplayAlert(string.Empty, "Please check your internet connection", "OK"); } } catch (Exception ex) { await DisplayAlert(string.Empty, "Something went wrong! Please try again", "OK"); } } else { await DisplayAlert(string.Empty, viewModel.ValidationErrors, "Cancel"); } }