UserLoginControl.cs
7.98 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using MoyaAdminLib;
using System.Web.Script.Serialization;
using System.Net;
using ThermoPrinterLibrary;
namespace MoyaSignup
{
public partial class UserLoginControl : UserControl
{
public event EventHandler LoginOk;
public delegate void ThermoPrinterEvent(ThermoPrinter.Models model);
public event ThermoPrinterEvent SetPrinter;
public User CurrentUser;
public UserLoginControl()
{
InitializeComponent();
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
BackColor = Color.Transparent;
infoLabel.Text = "Hei! Kirjoita sähköpostiosoitteesi";
}
private void UserLoginControl_Load(object sender, EventArgs e)
{
}
private void TxtEmail_TextChanged(object sender, EventArgs e)
{
var txt = txtEmail.Text;
if (IsValidEmail(txt))
{
timer1.Stop();
timer1.Start();
}
else
{
if (txtEmail.Text == "thermo2")
{
SetPrinter?.Invoke(ThermoPrinter.Models.IDP3210);
txtEmail.Text = "OK";
}
else if (txtEmail.Text == "thermo1")
{
SetPrinter?.Invoke(ThermoPrinter.Models.TMT188II);
txtEmail.Text = "OK";
}
timer1.Stop();
CurrentUser = null;
btnNewProfile.Visible = false;
infoLabel.Text = "Hei! Kirjoita sähköpostiosoitteesi";
}
}
private static bool IsValidEmail(string email)
{
if (!email.Contains("@") || !email.Contains("."))
return false;
var parts = email.Split(new[] {"@"}, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 2)
{
if (!parts[1].Contains("."))
return false;
var domainparts = parts[1].Split(new[] {"."}, StringSplitOptions.RemoveEmptyEntries);
if (domainparts.Length < 2)
return false;
var ltd = domainparts[domainparts.Length - 1];
if (ltd.Length < 2)
return false;
}
else
{
return false;
}
try
{
var addr = new System.Net.Mail.MailAddress(email);
return addr.Address == email;
}
catch
{
return false;
}
}
private void Timer1_Tick(object sender, EventArgs e)
{
var client = new RestClient();
try
{
var json = client.MakeRequest("user", "email=" + txtEmail.Text);
var ser = new JavaScriptSerializer();
var euser = ser.Deserialize<Eventuser>(json);
if (euser != null)
{
btnNewProfile.Visible = false;
CurrentUser = new User(euser);
infoLabel.Text = "Hei! " + CurrentUser + " Kirjoita salasanasi ja kirjaudu";
ShowLogin(true);
}
}
catch (WebException ex)
{
timer1.Stop();
var apiException = ex as MoyaApiException;
if (apiException != null && apiException.StatusCode == HttpStatusCode.NotFound)
{
CurrentUser = null;
infoLabel.Text = "Uusi kävijä?";
btnNewProfile.Visible = true;
ShowLogin(false);
}
else
{
Logger.Error("Connection to MoyaApi failed", ex);
MessageBox.Show(
"Yhteys taustajärjestelmään epäonnistui. Ota yhteys infoon.",
"Järjestelmävirhe",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
}
catch (Exception ex)
{
timer1.Stop();
Logger.Error("Unexpected exception", ex);
MessageBox.Show(
"Odottamaton virhe. Ota yhteys infoon.",
"Järjestelmävirhe",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
}
private void ShowLogin(bool show)
{
passwordTextBox.Visible = show;
loginButton.Visible = show;
}
private void Login()
{
var client = new RestClient(RestClient.ApiURL, HttpVerb.POST);
//client.PostData = "password=" + WebUtility.UrlEncode(passwordTextBox.Text);
client.PostData = "password=" + passwordTextBox.Text;
client.ContentType = "application/x-www-form-urlencoded";
try
{
var json = client.MakeRequest("v2/user/" + CurrentUser.UserId + "/check-password");
// string json2 = client.MakeRequest("user/"+ CurrentUser.UserId +"/check-password");
// MoyaWeb/rest/user/eventusers
var ser = new JavaScriptSerializer();
var euser = ser.Deserialize<Eventuser>(json);
if (euser != null)
{
CurrentUser = new User(euser)
{
Email = txtEmail.Text
};
LoginOk?.Invoke(this, null);
}
else
{
WrongPass();
}
}
catch (Exception)
{
WrongPass();
}
}
private void LoginButton_Click(object sender, EventArgs e)
{
Login();
}
private void WrongPass()
{
infoLabel.Text = "Väärä salasana. Syötä salasana uudelleen";
}
private void PasswordTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
Login();
}
private void BtnNewProfile_Click(object sender, EventArgs e)
{
CurrentUser = new User
{
Email = txtEmail.Text
};
//Send info to parent control
LoginOk?.Invoke(this, null);
}
internal void Clear()
{
CurrentUser = null;
txtEmail.Text = "";
passwordTextBox.Visible = false;
passwordTextBox.Text = "";
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
BackColor = Color.Transparent;
infoLabel.Text = "Hei! Kirjoita sähköpostiosoitteesi";
loginButton.Visible = false;
}
private void UserLoginControl_KeyUp(object sender, KeyEventArgs e)
{
/*if (e.Alt && e.Control && e.KeyCode == Keys.S)
{
Application.Exit();
return;
}
if (e.Alt && e.KeyCode == Keys.F5)
{
//userDetailsEditor1.SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.IDP3210);
//MessageBox.Show("Printteri: IDP3210");
}*/
}
private void TxtEmail_KeyUp(object sender, KeyEventArgs e)
{
/*if (e.Alt && e.Control && e.KeyCode == Keys.S)
{
Program.form1.AllowShutdown = true;
Application.Exit();
return;
}
if (e.Alt && e.KeyCode == Keys.F5)
{
//userDetailsEditor1.SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.IDP3210);
//MessageBox.Show("Printteri: IDP3210");
}*/
}
}
}