-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsetNewPassword.php
More file actions
185 lines (157 loc) · 7.69 KB
/
Copy pathsetNewPassword.php
File metadata and controls
185 lines (157 loc) · 7.69 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
<?php
include "connection.php";
// Checking Reset Password Link
if (isset($_GET["token"])) {
$stmt = $connection->prepare("SELECT * FROM users WHERE reset_token = ?");
$stmt->bind_param("s", $_GET["token"]);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_assoc();
if ($result->num_rows === 1) {
if (strtotime($data["reset_expire"]) < time()) {
exit("Link has Expired.");
}
} else {
exit("Invalid Token.");
}
} else {
exit("Invalid or Missing Token.");
}
// Updating Password to Database
if (isset($_POST["resetPassword"])) {
if ($_POST["newPassword"] === $_POST["confirmPassword"]) {
$success_token = bin2hex(random_bytes(32));
$email = $data["email"];
$password = password_hash($_POST["confirmPassword"], PASSWORD_DEFAULT);
$updatePassword = $connection->prepare("UPDATE users SET password = ?, reset_token = NULL, reset_expire = NULL, reset_success = ? WHERE email = ?");
$updatePassword->bind_param("sss", $password, $success_token, $email);
$updatePassword->execute();
header("location:changePasswordSuccess.php?token=$success_token");
exit();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Set New Password – CEBS</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/7.0.1/css/all.min.css" integrity="sha512-2SwdPD6INVrV/lHTZbO2nodKhrnDdJK9/kg2XD1r9uGqPo1cUbujc+IYdlYdEErWNu69gVcYgdxlmVmzTWnetw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght@600;700;800&family=DM+Sans:wght@300;400;500;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="auth.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<?php
$showNavbar = false;
include "header.php";
?>
<div class="reg-card">
<form method="POST" autocomplete="off">
<div class="screen active">
<h2>Set New Password</h2>
<p class="form-hint">Create a strong new password for your account. Minimum 8 characters recommended.</p>
<div class="form-divider"></div>
<label class="field-label">New Password</label>
<div class="input-wrap">
<i class="bi bi-lock icon-left"></i>
<input type="password" name="newPassword" class="form-ctrl" id="newPassField" placeholder="Min. 8 characters" required>
<i class="bi bi-eye icon-right" onclick="togglePass('newPassField', this)"></i>
<small class="d-none" id="passwordError"></small>
</div>
<label class="field-label">Confirm New Password</label>
<div class="input-wrap">
<i class="bi bi-lock icon-left"></i>
<input type="password" name="confirmPassword" class="form-ctrl" id="confirmPassField" placeholder="Re-enter password" required>
<i class="bi bi-eye icon-right" onclick="togglePass('confirmPassField', this)"></i>
<small class="d-none" id="confirmPasswordError"></small>
</div>
<button class="btn-submit btn-success-green" name="resetPassword" type="submit" id="resetPassword">Reset Password</button>
</div>
</div>
</form>
</div>
<?php include "footer.php"; ?>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
let passwordValid = false;
let confirmPasswordValid = false;
$("#newPassField").on("input", function() {
const password = $(this).val();
let regex = {
upper: /[A-Z]/,
lower: /[a-z]/,
digit: /[0-9]/,
special: /[!@#$%^&*(),.?":{}|<>]/,
length: /^.{8,}$/,
};
if (password.length === 0) {
$("#passwordError").text("Password is required.").css("color", "red").removeClass("d-none");
} else if (!regex.length.test(password)) {
$("#passwordError").text("Password must be at least 8 characters.").css("color", "red").removeClass("d-none");
} else if (!regex.upper.test(password)) {
$("#passwordError").text("Password must contain at least ONE uppercase letter.").css("color", "red").removeClass("d-none");
} else if (!regex.digit.test(password)) {
$("#passwordError").text("Password must contain at least ONE number.").css("color", "red").removeClass("d-none");
} else if (!regex.special.test(password)) {
$("#passwordError").text("Password must contain at least ONE special character.").css("color", "red").removeClass("d-none");
} else if (!regex.lower.test(password)) {
$("#passwordError").text("Password must contain at least ONE lowercase letter.").css("color", "red").removeClass("d-none");
} else {
$("#passwordError").addClass("d-none");
passwordValid = true;
}
passwordValid = false;
let confirmPassword = $("#confirmPassField").val();
if (confirmPassword === "") {
$("#confirmPasswordError").text("");
confirmPasswordValid = false;
} else if (confirmPassword !== password) {
$("#confirmPasswordError").text("Password Doesn't match.").css("color", "red").removeClass("d-none");
confirmPasswordValid = false;
} else {
$("#confirmPasswordError").addClass("d-none");
confirmPasswordValid = true;
return;
}
});
$("#confirmPassField").on("input", function() {
let confirmPassword = $(this).val();
let password = $("#newPassField").val();
if (confirmPassword === "") {
$("#confirmPasswordError").text("Password Doesn't match.").css("color", "red").removeClass("d-none");
confirmPasswordValid = false;
} else if (confirmPassword !== password) {
$("#confirmPasswordError").text("Password Doesn't match.").css("color", "red").removeClass("d-none");
confirmPasswordValid = false;
} else {
$("#confirmPasswordError").addClass("d-none");
confirmPasswordValid = true;
return;
}
confirmPasswordValid = false;
});
$(".btn-submit").on("click", function(e) {
if ($("#passwordError").is(":visible") || $("#confirmPasswordError").is(":visible")) {
e.preventDefault();
alert("Please fix the highlighted errors");
}
});
</script>
<script>
/* ── Password toggle ── */
function togglePass(inputId, iconEl) {
const input = document.getElementById(inputId);
if (input.type === 'password') {
input.type = 'text';
iconEl.classList.replace('bi-eye', 'bi-eye-slash');
} else {
input.type = 'password';
iconEl.classList.replace('bi-eye-slash', 'bi-eye');
}
}
</script>
</body>
</html>