-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforget_password.php
247 lines (227 loc) · 10 KB
/
forget_password.php
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
<?php
session_start();
include 'config.php';
include 'mail_config.php';
$message = '';
$message_type = '';
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['reset_password'])) {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$message = 'Invalid email format!';
$message_type = 'error';
} else {
// Check if the email exists in the database
$stmt = $conn->prepare("SELECT id FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows == 1) {
$user = $result->fetch_assoc();
$user_id = $user['id'];
// Generate a unique token
$token = bin2hex(random_bytes(50));
$expires = date("Y-m-d H:i:s", strtotime("+1 hour"));
// Store the token in the database
$stmt = $conn->prepare("INSERT INTO password_resets (email, token, expires) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $email, $token, $expires);
$stmt->execute();
$stmt->close();
// Send the password reset email
$mail = configureMailer();
$mail->addAddress($email);
$mail->Subject = 'Password Reset Request - SignEase';
// HTML Email Template
$reset_link = "http://localhost/project_signease/reset_password.php?token=$token";
$mail->Body = "
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
color: #333;
margin: 0;
padding: 0;
}
.email-container {
max-width: 600px;
margin: 20px auto;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
overflow: hidden;
}
.email-header {
background-color: #8B0000;
color: #fff;
text-align: center;
padding: 20px;
}
.email-header h1 {
margin: 0;
font-size: 24px;
}
.email-body {
padding: 20px;
}
.email-body p {
font-size: 16px;
line-height: 1.6;
}
.email-body a {
display: inline-block;
margin: 20px 0;
padding: 10px 20px;
background-color: #8B0000;
color: #fff;
text-decoration: none;
border-radius: 5px;
}
.email-footer {
background-color: #f4f4f4;
text-align: center;
padding: 10px;
font-size: 14px;
color: #666;
}
</style>
</head>
<body>
<div class='email-container'>
<div class='email-header'>
<h1>Password Reset Request</h1>
</div>
<div class='email-body'>
<p>Hello,</p>
<p>We received a request to reset your password for your SignEase account. Click the button below to reset your password:</p>
<a href='$reset_link'>Reset Password</a>
<p>If you did not request a password reset, please ignore this email or contact support if you have any questions.</p>
<p>This link will expire in 1 hour.</p>
</div>
<div class='email-footer'>
<p>Thank you,<br>The SignEase Team</p>
</div>
</div>
</body>
</html>
";
// Set email content type to HTML
$mail->isHTML(true);
if ($mail->send()) {
$message = 'A password reset link has been sent to your email.';
$message_type = 'success';
} else {
$message = 'Failed to send the password reset email.';
$message_type = 'error';
}
} else {
$message = 'No user found with this email.';
$message_type = 'error';
}
}
// Return a JSON response for AJAX
echo json_encode([
'message' => $message,
'message_type' => $message_type
]);
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Forgot Password - SignEase</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet">
<script>
tailwind.config = {
darkMode: 'class',
theme: {
extend: {
colors: {
primary: '#8B0000',
secondary: '#FFA500',
}
}
}
}
</script>
<style>
.spinner {
border: 3px solid #f3f3f3;
border-top: 3px solid #8B0000;
border-radius: 50%;
width: 20px;
height: 20px;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</head>
<body class="min-h-screen flex items-center justify-center p-6 bg-gray-100 dark:bg-gray-900 transition-colors duration-300">
<div class="bg-white dark:bg-gray-800 rounded-3xl shadow-2xl overflow-hidden w-full max-w-md">
<div class="p-8">
<h2 class="text-2xl font-semibold mb-6 relative dark:text-white">Forgot Password</h2>
<div id="message-container" class="hidden p-4 rounded-md shadow-md z-50 mb-4"></div>
<form id="reset-form" class="space-y-4">
<div class="relative">
<i class="fas fa-envelope absolute left-3 top-1/2 transform -translate-y-1/2 text-primary dark:text-white"></i>
<input type="email" name="email" id="email" placeholder="Input email" required class="w-full p-2 pl-10 border-b-2 border-gray-300 focus:border-primary outline-none dark:bg-gray-700 dark:text-white dark:border-gray-600">
</div>
<button type="submit" id="reset-button" class="w-full bg-primary text-white p-2 rounded-md hover:bg-secondary transition duration-300 dark:bg-primary dark:hover:bg-secondary flex items-center justify-center">
<span id="button-text">Reset Password</span>
<div id="spinner" class="spinner hidden ml-2"></div>
</button>
</form>
<p class="mt-4 text-sm dark:text-white">Remember your password? <a href="http://localhost/project_signease/login_register.php" class="text-primary dark:text-white hover:underline">Login now</a></p>
</div>
</div>
<script>
document.getElementById('reset-form').addEventListener('submit', async function (event) {
event.preventDefault(); // Prevent the default form submission
const button = document.getElementById('reset-button');
const buttonText = document.getElementById('button-text');
const spinner = document.getElementById('spinner');
const messageContainer = document.getElementById('message-container');
// Disable the button and show the spinner
button.disabled = true;
buttonText.textContent = 'Processing...';
spinner.classList.remove('hidden');
// Get the form data
const formData = new FormData(this);
formData.append('reset_password', 'true');
try {
// Send the form data via AJAX
const response = await fetch('forget_password.php', {
method: 'POST',
body: formData
});
const result = await response.json();
// Display the message
messageContainer.textContent = result.message;
messageContainer.classList.remove('hidden');
messageContainer.className = `p-4 rounded-md shadow-md z-50 ${
result.message_type === 'success' ? 'bg-green-100 text-green-700 border-green-400' :
result.message_type === 'error' ? 'bg-red-100 text-red-700 border-red-400' :
'bg-yellow-100 text-yellow-700 border-yellow-400'
}`;
} catch (error) {
console.error('Error:', error);
messageContainer.textContent = 'An error occurred. Please try again.';
messageContainer.classList.remove('hidden');
messageContainer.className = 'p-4 rounded-md shadow-md z-50 bg-red-100 text-red-700 border-red-400';
} finally {
// Re-enable the button and hide the spinner
button.disabled = false;
buttonText.textContent = 'Reset Password';
spinner.classList.add('hidden');
}
});
</script>
</body>
</html>