summaryrefslogtreecommitdiff
path: root/Juick/ViewModels/LoginViewModel.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Juick/ViewModels/LoginViewModel.cs')
-rw-r--r--Juick/ViewModels/LoginViewModel.cs72
1 files changed, 72 insertions, 0 deletions
diff --git a/Juick/ViewModels/LoginViewModel.cs b/Juick/ViewModels/LoginViewModel.cs
new file mode 100644
index 0000000..95528fe
--- /dev/null
+++ b/Juick/ViewModels/LoginViewModel.cs
@@ -0,0 +1,72 @@
+using System;
+using System.Net;
+using System.Windows;
+using Juick.Classes;
+using Juick.ViewModels.Validation;
+using RestSharp;
+
+namespace Juick.ViewModels
+{
+ public class LoginViewModel : DataViewModelBase
+ {
+ private string _username;
+ private string _password;
+ private DelegateCommand<RoutedEventArgs> _signInCommand;
+
+ public string Username
+ {
+ get
+ {
+ return _username ?? (_username = App.AppContext.Account.UserName);
+ }
+ set
+ {
+ _username = value;
+ NotifyPropertyChanged("Username");
+ }
+ }
+
+ public string Password
+ {
+ get
+ {
+ return _password ?? (_password = App.AppContext.Account.Password);
+ }
+ set
+ {
+ _password = value;
+ NotifyPropertyChanged("Password");
+ }
+ }
+
+ public Uri NextUri { get; set; }
+
+ public DelegateCommand<RoutedEventArgs> SignInCommand
+ {
+ get
+ {
+ return _signInCommand ?? (_signInCommand = new DelegateCommand<RoutedEventArgs>(CheckAuth, () => true));
+ }
+ }
+
+ public void CheckAuth(RoutedEventArgs e)
+ {
+ App.AppContext.Client.Authenticator = new HttpBasicAuthenticator(Username, Password);
+ App.AppContext.Client.ExecuteAsync(new RestRequest("/post", Method.POST), response =>
+ {
+ if (response.StatusCode ==
+ HttpStatusCode.BadRequest)
+ {
+ App.AppContext.Account.UserName = Username;
+ App.AppContext.Account.Password = Password;
+ App.AppContext.Account.IsAuthenticated = true;
+ ((App)Application.Current).NavigateTo(NextUri, true);
+ }
+ else
+ {
+ AddError("Username", "Invalid username or password", false);
+ }
+ });
+ }
+ }
+}