-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
166 lines (149 loc) · 6.22 KB
/
Form1.cs
File metadata and controls
166 lines (149 loc) · 6.22 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
using Pharmacy_Management_Sysytem.BusinessLogic;
using Pharmacy_Management_Sysytem.Common;
using Pharmacy_Management_Sysytem.Models;
using Pharmacy_Management_Sysytem.Services;
namespace Pharmacy_Management_Sysytem
{
/// <summary>
/// Sign-in form for user authentication
/// </summary>
public partial class signInForm : Form
{
private readonly IUserService _userService;
/// <summary>
/// Initializes a new instance of the signInForm class
/// </summary>
public signInForm()
{
InitializeComponent();
_userService = Program.Services.UserService;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Color Black = Color.Black;
Pen pen = new Pen(Black);
pen.Width = 10;
e.Graphics.DrawLine(pen, 500, 50, 500, 510);
}
/// <summary>
/// Handles the sign-in button click event
/// </summary>
/// <param name="sender">The event sender</param>
/// <param name="e">The event arguments</param>
private void btnSignIn_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrWhiteSpace(txtBoxUserName.Text) || string.IsNullOrWhiteSpace(txtBoxPassword.Text))
{
MessageBox.Show("Please enter both username and password.", "Validation Error",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// Check if this is the first time (no users in database) - use default admin
var allUsersResult = _userService.GetAllUsers();
if (allUsersResult.IsSuccess && allUsersResult.Data.Count == 0)
{
// First time setup - use default admin credentials
if (txtBoxUserName.Text == Configuration.DefaultAdminUsername &&
txtBoxPassword.Text == Configuration.DefaultAdminPassword)
{
SaveCredentialsToRegistry();
var adminForm = new adminstratorForm();
adminForm.Show();
this.Hide();
return;
}
else
{
MessageBox.Show("Wrong User Name Or Password.", "Authentication Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
// Authenticate user using the service layer
var authResult = _userService.AuthenticateUser(txtBoxUserName.Text, txtBoxPassword.Text);
if (authResult.IsSuccess)
{
var user = authResult.Data;
SaveCredentialsToRegistry();
if (user.UserRole == UserRole.Administrator)
{
var adminForm = new adminstratorForm(user.UserName);
adminForm.Show();
this.Hide();
}
else if (user.UserRole == UserRole.Pharmacist)
{
var pharmacistForm = new PharmacistForm();
pharmacistForm.Show();
this.Hide();
}
else
{
MessageBox.Show("Invalid user role.", "Authentication Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show($"Authentication failed: {authResult.ErrorMessage}\n\nPlease check:\n1. Database connection\n2. Username and password\n3. Database has users table with data", "Authentication Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred during sign-in: {ex.Message}", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Saves user credentials to the Windows registry for auto-login
/// </summary>
private void SaveCredentialsToRegistry()
{
try
{
string keyPath = @"HKEY_CURRENT_USER\SOFTWARE\Pharmacy";
Registry.SetValue(keyPath, "UserName", txtBoxUserName.Text, RegistryValueKind.String);
Registry.SetValue(keyPath, "Password", txtBoxPassword.Text, RegistryValueKind.String);
}
catch (Exception ex)
{
// Log the error but don't show to user as this is not critical
System.Diagnostics.Debug.WriteLine($"Failed to save credentials to registry: {ex.Message}");
}
}
private void signInForm_Load(object sender, EventArgs e)
{
string keyPath = @"HKEY_CURRENT_USER\SOFTWARE\Pharmacy";
string userName = "UserName";
string passWord = "Password";
try
{
string userData = Registry.GetValue(keyPath, userName, null) as string;
string passWordData = Registry.GetValue(keyPath, passWord, null) as string;
if (userData != null)
{
txtBoxUserName.Text = userData;
}
if (passWordData != null)
{
txtBoxPassword.Text = passWordData;
}
}
catch {
}
}
}
}