public async Task LogoutAsync() { bool logout = await _dialogService.ShowConfirmationDialogAsync( "Confirmation", "Are you sure you want to log out?", "Yes", "No"); if (logout) { //TODO: DELETE ALL !! //delete all from the db //delete user settings //delete all view models OpenPane(false); ShowLoading(true, "Logging out... Please wait.."); BackgroundTasksManager.UnregisterBackgroundTask(); _appSettings.ResetAppSettings(); await _dataService .UserService .ChangeCurrentUserStatus(false); string currentLoggedUsername = _userCredentialService.GetCurrentLoggedUsername(); if (string.IsNullOrEmpty(currentLoggedUsername)) { currentLoggedUsername = _userCredentialService.DefaultUsername; } _userCredentialService.DeleteUserCredential(ResourceType.ALL, currentLoggedUsername); _navigationService.GoBack(); ShowLoading(false); } }
protected override async Task NavigateToFirstViewModel(object hint = null) { var response = await _dataService .UserService .GetCurrentActiveUserAsync(); var loggedUsername = _userCredentialService.GetCurrentLoggedUsername(); var isUserLoggedIn = loggedUsername != _userCredentialService.DefaultUsername && !string.IsNullOrEmpty(loggedUsername); if (!response.Succeed || isUserLoggedIn && response.Result is null) { var errorMsg = string.IsNullOrEmpty(response.Message) ? $"Did you uninstall the app without signing out ?{System.Environment.NewLine}I will properly log you out now..." : response.Message; _userCredentialService.DeleteUserCredential( ResourceType.ALL, _userCredentialService.DefaultUsername); _logger.Warning( $"{nameof(NavigateToFirstViewModel)}: Couldnt get a user in the db = {response.Succeed} " + $"or isUserLoggedIn and no user exists in db. {errorMsg}"); await NavigationService.Navigate <LoginViewModel>(); return; } if (isUserLoggedIn && _appSettings.AskForPasswordWhenAppStarts) { //_dialogService.ShowLoginDialog(async password => //{ // var result = await PasswordMatches(password); // if (result) // { // await NavigationService.Navigate<MainViewModel>(); // } // else // { // await NavigationService.Navigate<LoginViewModel>(); // } //}); } else if (isUserLoggedIn) { _dialogService.ShowSucceedToast("navegando..."); await NavigationService.Navigate <MainViewModel>(); } else { await NavigationService.Navigate <LoginViewModel>(); } }
protected override async Task <HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { // Cloning the request, in case we need to send it again var clonedRequest = await CloneRequest(request); var response = await base.SendAsync(clonedRequest, cancellationToken); if (response.StatusCode == HttpStatusCode.Unauthorized) { try { var currentLoggedUsername = _userCredentialService.GetCurrentLoggedUsername(); if (string.IsNullOrEmpty(currentLoggedUsername)) { await _dialogService.ShowMessageDialogAsync( "Error", "Could't retrieve the current logged username"); return(response); } var refreshToken = _userCredentialService.GetUserCredential( ResourceType.REFRESH_TOKEN_RESOURCE, currentLoggedUsername); if (string.IsNullOrEmpty(refreshToken)) { await _dialogService.ShowMessageDialogAsync( "Error", "Could't retrieve the refresh token"); return(response); } var tokenResponse = await _googleAuthService.GetNewTokenAsync(refreshToken); if (!tokenResponse.Succeed) { await _dialogService.ShowMessageDialogAsync( "Error", "Could't get a new token. Did you remove access to our app :C?"); return(response); } var token = tokenResponse.Result; // we're now logged in again. // Clone the request clonedRequest = await CloneRequest(request); // Save the user to the app settings _userCredentialService.UpdateUserCredential( ResourceType.REFRESH_TOKEN_RESOURCE, currentLoggedUsername, false, token.RefreshToken); _userCredentialService.UpdateUserCredential( ResourceType.TOKEN_RESOURCE, currentLoggedUsername, false, token.AccessToken); // Set the authentication header //clonedRequest.Headers.Remove("Bearer"); //clonedRequest.Headers.Add("Bearer", newToken.AccessToken); clonedRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken); // Resend the request response = await base.SendAsync(clonedRequest, cancellationToken); } catch (InvalidOperationException) { // user cancelled auth, so lets return the original response return(response); } } return(response); }