-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm3.cs
More file actions
140 lines (116 loc) · 4.94 KB
/
Copy pathForm3.cs
File metadata and controls
140 lines (116 loc) · 4.94 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace H1
{
public partial class Form3 : Form
{
// Connection string - replace with your actual server details
private readonly string connectionString = @"Data Source=ABDULSABOOR190\SQLEXPRESS;Initial Catalog=sabbbb;Integrated Security=True;Encrypt=False";
public Form3()
{
InitializeComponent();
}
private void role_Combobox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Email_textBox_TextChanged(object sender, EventArgs e)
{
}
private void Password_textbox_TextChanged(object sender, EventArgs e)
{
}
private void bttnLogin_Click(object sender, EventArgs e)
{
string email = Email_textBox.Text.Trim();
string password = Password_textbox.Text;
string roleSelected = role_Combobox.SelectedItem.ToString();
// Convert the role to match the format in your database (lowercase with underscores)
string role = ConvertRoleFormat(roleSelected);
if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
{
MessageBox.Show("Please enter both email and password.", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Check if login credentials are valid
if (ValidateLogin(email, password, role))
{
MessageBox.Show($"Login successful! Welcome {roleSelected}.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
// TODO: Navigate to appropriate form based on role
// For example:
// if (role == "traveler")
// {
// Form4 travelerDashboard = new Form4();
// travelerDashboard.Show();
// }
}
else
{
MessageBox.Show("Invalid email or password or your account hasn't been approved yet.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private string ConvertRoleFormat(string displayRole)
{
// Convert from display format (e.g., "Service Provider") to database format (e.g., "service_provider")
switch (displayRole)
{
case "Traveler":
return "traveler";
case "Operator":
return "operator";
case "Admin":
return "admin";
case "Service Provider":
return "service_provider";
default:
return displayRole.ToLower();
}
}
private bool ValidateLogin(string email, string password, string role)
{
bool isValid = false;
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// SQL query to check if user exists with given credentials, role, and is active and approved
string query = @"SELECT COUNT(1) FROM AppUser
WHERE email = @Email
AND password_hash = @Password
AND role = @Role
AND is_active = 1
AND registration_status = 'approved'";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@Email", email);
command.Parameters.AddWithValue("@Password", password); // In production, implement proper password validation
command.Parameters.AddWithValue("@Role", role);
int count = (int)command.ExecuteScalar();
isValid = (count > 0);
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Database error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return isValid;
}
private void Register_here_link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
// Navigate to registration form
// For example:
// Form2 registrationForm = new Form2();
// registrationForm.Show();
MessageBox.Show("Registration functionality will be implemented soon.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}