skapade registrering, logga in och logga ut. ändrade lite i auth.php
This commit is contained in:
@@ -1,7 +1,23 @@
|
||||
<?php
|
||||
session_start();
|
||||
require_once($_SERVER['DOCUMENT_ROOT'] . '/db.php');
|
||||
|
||||
if (!isset($_SESSION['user_id'])) {
|
||||
function isAuthenticated() {
|
||||
if (!isset($_SESSION['user_id']) || !isset($_SESSION['uniqueid'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$conn = getConnection();
|
||||
$stmt = $conn->prepare("SELECT id FROM users WHERE id = :id AND uniqueid = :uniqueid");
|
||||
$stmt->execute([
|
||||
'id' => $_SESSION['user_id'],
|
||||
'uniqueid' => $_SESSION['uniqueid'],
|
||||
]);
|
||||
|
||||
return $stmt->fetch() !== false;
|
||||
}
|
||||
|
||||
if (!isAuthenticated()) {
|
||||
header("Location: /landing.php");
|
||||
exit();
|
||||
}
|
||||
|
||||
43
inc/php/login.php
Normal file
43
inc/php/login.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
session_start();
|
||||
require_once($_SERVER['DOCUMENT_ROOT'] . '/db.php');
|
||||
|
||||
$errors = [];
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$user = trim($_POST['user'] ?? '');
|
||||
$password = $_POST['password'] ?? '';
|
||||
|
||||
if (empty($user) || empty($password)) {
|
||||
$errors[] = "All fields are required.";
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
$conn = getConnection();
|
||||
$stmt = $conn->prepare("SELECT id, password, uniqueid FROM users WHERE email = :user OR username = :user");
|
||||
$stmt->execute(['user' => $user]);
|
||||
$result = $stmt->fetch();
|
||||
|
||||
if ($result && password_verify($password, $result['password'])) {
|
||||
$_SESSION['user_id'] = $result['id'];
|
||||
$_SESSION['uniqueid'] = $result['uniqueid'];
|
||||
header("Location: /home.php");
|
||||
exit();
|
||||
} else {
|
||||
$errors[] = "Invalid credentials.";
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
<!-- Basic form UI -->
|
||||
<h2>Login</h2>
|
||||
<form method="POST">
|
||||
<input name="user" placeholder="Username or Email" required><br>
|
||||
<input name="password" type="password" placeholder="Password" required><br>
|
||||
<button type="submit">Login</button>
|
||||
</form>
|
||||
|
||||
<?php if (!empty($errors)): ?>
|
||||
<ul><?php foreach ($errors as $e) echo "<li>$e</li>"; ?></ul>
|
||||
<?php endif; ?>
|
||||
67
inc/php/register.php
Normal file
67
inc/php/register.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
session_start();
|
||||
require_once($_SERVER['DOCUMENT_ROOT'] . '/db.php');
|
||||
|
||||
$errors = [];
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$username = trim($_POST['username'] ?? '');
|
||||
$email = trim($_POST['email'] ?? '');
|
||||
$password = $_POST['password'] ?? '';
|
||||
|
||||
// Validate inputs
|
||||
if (empty($username) || empty($email) || empty($password)) {
|
||||
$errors[] = "All fields are required.";
|
||||
}
|
||||
|
||||
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
$errors[] = "Invalid email format.";
|
||||
}
|
||||
|
||||
if (strlen($password) < 6) {
|
||||
$errors[] = "Password must be at least 6 characters.";
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
$conn = getConnection();
|
||||
|
||||
// Check if email or username already exists
|
||||
$stmt = $conn->prepare("SELECT id FROM users WHERE email = :email OR username = :username");
|
||||
$stmt->execute(['email' => $email, 'username' => $username]);
|
||||
|
||||
if ($stmt->fetch()) {
|
||||
$errors[] = "Email or username already in use.";
|
||||
} else {
|
||||
$hash = password_hash($password, PASSWORD_BCRYPT, ['cost' => 12]);
|
||||
$uniqueId = bin2hex(random_bytes(16)); // session ID
|
||||
|
||||
$insert = $conn->prepare("INSERT INTO users (username, email, password, uniqueid) VALUES (:username, :email, :password, :uniqueid)");
|
||||
$insert->execute([
|
||||
'username' => $username,
|
||||
'email' => $email,
|
||||
'password' => $hash,
|
||||
'uniqueid' => $uniqueId,
|
||||
]);
|
||||
|
||||
$_SESSION['user_id'] = $conn->lastInsertId();
|
||||
$_SESSION['uniqueid'] = $uniqueId;
|
||||
|
||||
header("Location: /home.php");
|
||||
exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
<!-- Basic form UI -->
|
||||
<h2>Register</h2>
|
||||
<form method="POST">
|
||||
<input name="username" placeholder="Username" required><br>
|
||||
<input name="email" type="email" placeholder="Email" required><br>
|
||||
<input name="password" type="password" placeholder="Password" required><br>
|
||||
<button type="submit">Register</button>
|
||||
</form>
|
||||
|
||||
<?php if (!empty($errors)): ?>
|
||||
<ul><?php foreach ($errors as $e) echo "<li>$e</li>"; ?></ul>
|
||||
<?php endif; ?>
|
||||
Reference in New Issue
Block a user