summaryrefslogtreecommitdiff
path: root/Juick/ViewModels/LoginViewModel.cs
blob: c0bf1f91ea3fe44b1b4a73e0254559bc2d0a1266 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Net;
using System.Windows;
using Juick.Classes;
using RestSharp;

namespace Juick.ViewModels
{
    public class LoginViewModel : ViewModelBase 
    {
        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.AppContext.EnableNotifications();
                                                                                     ((App)Application.Current).NavigateTo(NextUri, true);
                                                                                 }
                                                                                 else
                                                                                 {
                                                                                     MessageBox.Show("Invalid username or password", "Error", MessageBoxButton.OK);
                                                                                 }
                                                                             });
        }
    }
}