-
Notifications
You must be signed in to change notification settings - Fork 5
/
process-reset-password.php
60 lines (39 loc) · 1.24 KB
/
process-reset-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
<?php
$token = $_POST["token"];
$token_hash = hash("sha256", $token);
$mysqli = require __DIR__ . "/database.php";
$sql = "SELECT * FROM user
WHERE reset_token_hash = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $token_hash);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
if ($user === null) {
die("token not found");
}
if (strtotime($user["reset_token_expires_at"]) <= time()) {
die("token has expired");
}
if (strlen($_POST["password"]) < 8) {
die("Password must be at least 8 characters");
}
if ( ! preg_match("/[a-z]/i", $_POST["password"])) {
die("Password must contain at least one letter");
}
if ( ! preg_match("/[0-9]/", $_POST["password"])) {
die("Password must contain at least one number");
}
if ($_POST["password"] !== $_POST["password_confirmation"]) {
die("Passwords must match");
}
$password_hash = password_hash($_POST["password"], PASSWORD_DEFAULT);
$sql = "UPDATE user
SET password_hash = ?,
reset_token_hash = NULL,
reset_token_expires_at = NULL
WHERE id = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("ss", $password_hash, $user["id"]);
$stmt->execute();
echo "Password updated. You can now login.";