aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.WinForms/UniteLoginDialog.cs
blob: 4c850059c533452eb703e03634abacc4c3b50389 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ShiftOS.Engine;
using System.Net;

namespace ShiftOS.WinForms
{
    public partial class UniteLoginDialog : UserControl, IShiftOSWindow
    {
        public UniteLoginDialog(Action<string> callback)
        {
            InitializeComponent();
            Callback = callback;
        }

        private Action<string> Callback { get; set; }

        public void OnLoad()
        {
            this.ParentForm.AcceptButton = btnlogin;
        }

        public void OnSkinLoad()
        {
        }

        public bool OnUnload()
        {
            return true;
        }

        public void OnUpgrade()
        {
        }

        private void btnlogin_Click(object sender, EventArgs e)
        {
            string u = txtusername.Text;
            string p = txtpassword.Text;

            if (string.IsNullOrWhiteSpace(u))
            {
                Infobox.Show("Please enter a username.", "You must enter a proper email address.");
                return;
            }

            if (string.IsNullOrWhiteSpace(p))
            {
                Infobox.Show("Please enter a password.", "You must enter a valid password.");
                return;
            }

            try
            {
                var webrequest = HttpWebRequest.Create("http://getshiftos.ml/Auth/Login?appname=ShiftOS&appdesc=ShiftOS+client&version=1_0_beta_2_4");
                string base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{u}:{p}"));
                webrequest.Headers.Add("Authentication: Basic " + base64);
                var response = webrequest.GetResponse();
                var str = response.GetResponseStream();
                var reader = new System.IO.StreamReader(str);
                string result = reader.ReadToEnd();
                reader.Close();
                str.Close();
                str.Dispose();
                response.Dispose();
                Callback?.Invoke(result);
                AppearanceManager.Close(this);
            }
#if DEBUG
            catch(Exception ex)
            {
                Infobox.Show("Error", ex.ToString());
            }
#else
            catch
            {
                Infobox.Show("Login failed.", "The login attempt failed due to an incorrect username and password pair.");
            }
#endif

        }
    }
}