64 lines
2.3 KiB
PHP
64 lines
2.3 KiB
PHP
<?php
|
|
require_once '../includes/header.php';
|
|
require_once '../db/config.php';
|
|
require_once '../mail/MailService.php';
|
|
|
|
$message = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$email = $_POST['email'];
|
|
$pdo = db();
|
|
|
|
$stmt = $pdo->prepare("SELECT * FROM Users WHERE email = ?");
|
|
$stmt->execute([$email]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user) {
|
|
$token = bin2hex(random_bytes(50));
|
|
$expires = new DateTime('now');
|
|
$expires->add(new DateInterval('PT1H')); // 1 hour expiration
|
|
|
|
$stmt = $pdo->prepare("UPDATE Users SET reset_token = ?, reset_token_expires = ? WHERE email = ?");
|
|
$stmt->execute([$token, $expires->format('Y-m-d H:i:s'), $email]);
|
|
|
|
$reset_link = "http://" . $_SERVER['HTTP_HOST'] . "/auth/reset-password.php?token=" . $token;
|
|
|
|
$subject = "Password Reset Request";
|
|
$body_html = "Click the following link to reset your password: <a href='{$reset_link}'>{$reset_link}</a>";
|
|
$body_text = "Click the following link to reset your password: {$reset_link}";
|
|
|
|
MailService::sendMail($email, $subject, $body_html, $body_text);
|
|
|
|
$message = "A password reset link has been sent to your email address.";
|
|
} else {
|
|
$message = "No user found with that email address.";
|
|
}
|
|
}
|
|
?>
|
|
|
|
<div class="container">
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-6">
|
|
<div class="card mt-5">
|
|
<div class="card-header">
|
|
<h3>Forgot Password</h3>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if ($message): ?>
|
|
<div class="alert alert-info"><?php echo $message; ?></div>
|
|
<?php endif; ?>
|
|
<form action="forgot-password.php" method="POST">
|
|
<div class="form-group">
|
|
<label for="email">Email address</label>
|
|
<input type="email" class="form-control" id="email" name="email" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Send Password Reset Link</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once '../includes/footer.php'; ?>
|