Skip to content

Instantly share code, notes, and snippets.

@hamid-shaikh
Last active September 30, 2019 11:55
Show Gist options
  • Save hamid-shaikh/0774fe04cf7121c0d596925c93fc1809 to your computer and use it in GitHub Desktop.
Save hamid-shaikh/0774fe04cf7121c0d596925c93fc1809 to your computer and use it in GitHub Desktop.
Refit + Prism Forms + Fusillade + ModernHttpClient (LoginPageViewModel)
//Constructer Creation
public partial class LoginPageViewModel
{
public LoginPageViewModel(INavigationService navigationService,
IApiService<IAuthenticationService> authenticationService)
{
NavigationService = authenticationService;
AuthenticationService = authenticationService;//Here you will get AuthenticationService object injected
}
}
//Variables Declaration
public partial class LoginPageViewModel
{
IApiService<IAuthenticationService> AuthenticationService { get; set; }
INavigationService NavigationService { get; set; }
}
//Bindable Properties
public partial class LoginPageViewModel
{
private string userName;
public string UserName
{
get { return userName; }
set { SetProperty(ref userName, value); }
}
private string password;
public string Password
{
get { return password; }
set { SetProperty(ref password, value); }
}
}
//Commands Creations
public partial class LoginPageViewModel
{
public DelegateCommand LoginCommand => new DelegateCommand(OnLoginCommandExecuted);
}
//Private Methods
public partial class LoginPageViewModel
{
async void OnLoginCommandExecuted()
{
try
{
IsLoading = IsBusy = true;
//Calling IAuth Service, AuthenticateUser method to check user credentials are valid/not.
string userCredential = "Basic " + Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{UserName}:{Password}"));
var response = await AuthenticationService?
.UserInitiated?
.AuthenticateUserAsync<AuthenticationModel>(userCredential);
if (response?.Status == System.Net.HttpStatusCode.OK)
{
await NavigationService.NavigateAsync("../" + nameof(DashboardPage));
}
else
{
throw new UnauthorizedAccessException();
}
}
finally
{
IsLoading = IsBusy = false;
}
}
}