-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.php
More file actions
164 lines (144 loc) · 6.16 KB
/
auth.php
File metadata and controls
164 lines (144 loc) · 6.16 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
<?php
require_once ("./core/core.php");
$config = getCore()->getConfig();
$conn = getCore()->getConn();
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$login = $_POST["login"];
$password = $_POST["password"];
if (isset($_POST['register'])) {
$sql = "SELECT * FROM Users WHERE username = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $login);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
$id = rand(1, 10000);
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$sql = "INSERT INTO Users (username, password, id) VALUES (?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sss", $login, $hashedPassword, $id);
if ($stmt->execute()) {
$_SESSION['id'] = $stmt->insert_id;
header("Location: /todo.php");
exit;
}
}
} else {
$sql = "SELECT * FROM Users WHERE username = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $login);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 1) {
$row = $result->fetch_assoc();
$hashedPassword = $row["password"];
if (password_verify($password, $hashedPassword)) {
$_SESSION['id'] = $row['id'];
header("Location: /todo.php");
exit;
}
}
}
}
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="preconnect" href="https://fonts.gstatic.com">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;500;600&display=swap" rel="stylesheet">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="core/public/css/core.css">
<link rel="stylesheet" href="/scripts/auth.css">
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<title>TodoForYou</title>
</head>
<body>
<div class="app">
<? require_once("./core/blocks/navbar.php") ?>
<div class="pre-body">
<div class="container">
<div class="forms">
<div class="form login">
<span class="title">Логин</span>
<form method="post">
<div class="input-field">
<input type="text" name="login" placeholder="Enter your login" required>
<i class="uil uil-envelope icon"></i>
</div>
<div class="input-field">
<input type="password" name="password" class="password" placeholder="Enter your password" required>
<i class="uil uil-lock icon"></i>
<i class="uil uil-eye-slash showHidePw"></i>
</div>
<div class="input-field button">
<input type="submit" value="Login">
</div>
</form>
<div class="login-signup">
<span class="text">Нет аккаунта?
<a href="#" class="text signup-link">Зарегистрироваться</a>
</span>
</div>
</div>
<div class="form signup">
<span class="title">Регистрация</span>
<form method="post">
<input type="hidden" name="register" value="true">
<div class="input-field">
<input type="text" name="login" placeholder="Enter your login" required>
<i class="uil uil-user"></i>
</div>
<div class="input-field">
<input type="password" name="password" class="password" placeholder="Create a password" required>
<i class="uil uil-lock icon"></i>
</div>
<div class="input-field button">
<input type="submit" value="Signup">
</div>
</form>
<div class="login-signup">
<span class="text">Уже есть аккаунт?
<a href="#" class="text login-link">Войти</a>
</span>
</div>
</div>
</div>
</div>
</div>
<script>
const container = document.querySelector(".container"),
pwShowHide = document.querySelectorAll(".showHidePw"),
pwFields = document.querySelectorAll(".password"),
signUp = document.querySelector(".signup-link"),
login = document.querySelector(".login-link");
pwShowHide.forEach(eyeIcon =>{
eyeIcon.addEventListener("click", ()=>{
pwFields.forEach(pwField =>{
if(pwField.type ==="password"){
pwField.type = "text";
pwShowHide.forEach(icon =>{
icon.classList.replace("uil-eye-slash", "uil-eye");
})
}else{
pwField.type = "password";
pwShowHide.forEach(icon =>{
icon.classList.replace("uil-eye", "uil-eye-slash");
})
}
})
})
})
signUp.addEventListener("click", ( )=>{
container.classList.add("active");
});
login.addEventListener("click", ( )=>{
container.classList.remove("active");
});
</script>
</div>
</body>
</html>