1
This commit is contained in:
parent
18598f7de7
commit
3db6af498b
21
db/migrate.php
Normal file
21
db/migrate.php
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/config.php';
|
||||||
|
|
||||||
|
function run_migrations() {
|
||||||
|
try {
|
||||||
|
$pdo = db();
|
||||||
|
$migration_file = __DIR__ . '/migrations/001_create_users_table.sql';
|
||||||
|
if (file_exists($migration_file)) {
|
||||||
|
$sql = file_get_contents($migration_file);
|
||||||
|
$pdo->exec($sql);
|
||||||
|
echo "Migration '001_create_users_table.sql' applied successfully.\n";
|
||||||
|
} else {
|
||||||
|
echo "Migration file not found.\n";
|
||||||
|
}
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
die("Migration failed: " . $e->getMessage() . "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
run_migrations();
|
||||||
|
|
||||||
6
db/migrations/001_create_users_table.sql
Normal file
6
db/migrations/001_create_users_table.sql
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
username VARCHAR(50) NOT NULL UNIQUE,
|
||||||
|
password_hash VARCHAR(255) NOT NULL,
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
78
index.php
78
index.php
@ -1,3 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
|
||||||
|
// If the user is not logged in, redirect to the login page
|
||||||
|
if (!isset($_SESSION['user_id'])) {
|
||||||
|
header('Location: login.php');
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
?>
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
@ -45,37 +54,9 @@
|
|||||||
<input type="text" class="form-control rounded-pill" placeholder="Search or start new chat">
|
<input type="text" class="form-control rounded-pill" placeholder="Search or start new chat">
|
||||||
</div>
|
</div>
|
||||||
<div class="conversation-list">
|
<div class="conversation-list">
|
||||||
<!-- Active Conversation -->
|
<div class="text-center text-muted p-4">
|
||||||
<div class="conversation-item active">
|
<i class="bi bi-chat-dots fs-2"></i>
|
||||||
<img src="https://i.pravatar.cc/150?u=jane" alt="Jane Doe" class="avatar">
|
<p class="mt-2">No conversations yet.</p>
|
||||||
<div class="conversation-info flex-grow-1">
|
|
||||||
<div class="d-flex justify-content-between">
|
|
||||||
<span class="name">Jane Doe</span>
|
|
||||||
<span class="time small text-muted">10:42 AM</span>
|
|
||||||
</div>
|
|
||||||
<p class="last-message mb-0">Sounds good! See you then.</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<!-- Other Conversations -->
|
|
||||||
<div class="conversation-item">
|
|
||||||
<img src="https://i.pravatar.cc/150?u=john" alt="John Smith" class="avatar">
|
|
||||||
<div class="conversation-info flex-grow-1">
|
|
||||||
<div class="d-flex justify-content-between">
|
|
||||||
<span class="name">John Smith</span>
|
|
||||||
<span class="time small text-muted">Yesterday</span>
|
|
||||||
</div>
|
|
||||||
<p class="last-message mb-0">Can you send me the report?</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="conversation-item">
|
|
||||||
<img src="https://i.pravatar.cc/150?u=group" alt="Project Team" class="avatar">
|
|
||||||
<div class="conversation-info flex-grow-1">
|
|
||||||
<div class="d-flex justify-content-between">
|
|
||||||
<span class="name">Project Team</span>
|
|
||||||
<span class="time small text-muted">3/15/25</span>
|
|
||||||
</div>
|
|
||||||
<p class="last-message mb-0"><strong>Alex:</strong> Don't forget the meeting...</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</aside>
|
</aside>
|
||||||
@ -84,35 +65,26 @@
|
|||||||
<main class="chat-view">
|
<main class="chat-view">
|
||||||
<!-- Chat Header -->
|
<!-- Chat Header -->
|
||||||
<header class="chat-header">
|
<header class="chat-header">
|
||||||
<img src="https://i.pravatar.cc/150?u=jane" alt="Jane Doe" class="avatar avatar-sm me-3">
|
<div class="d-flex align-items-center">
|
||||||
<div>
|
<img src="https://i.pravatar.cc/150?u=placeholder" alt="Avatar" class="avatar avatar-sm me-3">
|
||||||
<h5 class="mb-0 name">Jane Doe</h5>
|
<div>
|
||||||
<p class="mb-0 status text-success">Online</p>
|
<h5 class="mb-0 name">Select a Conversation</h5>
|
||||||
|
<p class="mb-0 status text-muted">Offline</p>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="ms-auto">
|
<div class="ms-auto">
|
||||||
<button class="btn btn-light rounded-circle"><i class="bi bi-telephone"></i></button>
|
<button class="btn btn-light rounded-circle" disabled><i class="bi bi-telephone"></i></button>
|
||||||
<button class="btn btn-light rounded-circle mx-2"><i class="bi bi-camera-video"></i></button>
|
<button class="btn btn-light rounded-circle mx-2" disabled><i class="bi bi-camera-video"></i></button>
|
||||||
<button class="btn btn-light rounded-circle"><i class="bi bi-three-dots-vertical"></i></button>
|
<button class="btn btn-light rounded-circle" disabled><i class="bi bi-three-dots-vertical"></i></button>
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<!-- Chat Body -->
|
<!-- Chat Body -->
|
||||||
<div class="chat-body">
|
<div class="chat-body">
|
||||||
<div class="message received">
|
<div class="text-center text-muted" style="margin-top: auto; margin-bottom: auto;">
|
||||||
<div class="message-bubble">Hey! Are we still on for lunch tomorrow?</div>
|
<i class="bi bi-wechat fs-1"></i>
|
||||||
<div class="message-time">10:40 AM</div>
|
<h4 class="mt-3">Welcome to Message NOW</h4>
|
||||||
</div>
|
<p>Select a conversation to start messaging.</p>
|
||||||
<div class="message sent">
|
|
||||||
<div class="message-bubble">Hi! Yes, absolutely. I'm looking forward to it.</div>
|
|
||||||
<div class="message-time">10:41 AM</div>
|
|
||||||
</div>
|
|
||||||
<div class="message received">
|
|
||||||
<div class="message-bubble">Great! How about 1 PM at the usual place?</div>
|
|
||||||
<div class="message-time">10:41 AM</div>
|
|
||||||
</div>
|
|
||||||
<div class="message sent">
|
|
||||||
<div class="message-bubble">Sounds good! See you then.</div>
|
|
||||||
<div class="message-time">10:42 AM</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
69
login.php
Normal file
69
login.php
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
require_once 'db/config.php';
|
||||||
|
|
||||||
|
$error = '';
|
||||||
|
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
|
$username = trim($_POST['username']);
|
||||||
|
$password = $_POST['password'];
|
||||||
|
|
||||||
|
if (empty($username) || empty($password)) {
|
||||||
|
$error = 'Please enter both username and password.';
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
$pdo = db();
|
||||||
|
$stmt = $pdo->prepare('SELECT id, username, password_hash FROM users WHERE username = ?');
|
||||||
|
$stmt->execute([$username]);
|
||||||
|
$user = $stmt->fetch();
|
||||||
|
|
||||||
|
if ($user && password_verify($password, $user['password_hash'])) {
|
||||||
|
$_SESSION['user_id'] = $user['id'];
|
||||||
|
$_SESSION['username'] = $user['username'];
|
||||||
|
header('Location: index.php');
|
||||||
|
exit;
|
||||||
|
} else {
|
||||||
|
$error = 'Invalid username or password.';
|
||||||
|
}
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
$error = 'Database error: ' . $e->getMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Login</title>
|
||||||
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||||
|
<link rel="stylesheet" href="assets/css/custom.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container d-flex justify-content-center align-items-center vh-100">
|
||||||
|
<div class="card" style="width: 24rem;">
|
||||||
|
<div class="card-body">
|
||||||
|
<h1 class="card-title text-center mb-4">Login</h1>
|
||||||
|
<?php if ($error): ?>
|
||||||
|
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
|
||||||
|
<?php endif; ?>
|
||||||
|
<form action="login.php" method="POST">
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="username" class="form-label">Username</label>
|
||||||
|
<input type="text" class="form-control" id="username" name="username" required>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="password" class="form-label">Password</label>
|
||||||
|
<input type="password" class="form-control" id="password" name="password" required>
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-primary w-100">Log In</button>
|
||||||
|
</form>
|
||||||
|
<div class="text-center mt-3">
|
||||||
|
<p>Don't have an account? <a href="signup.php">Sign Up</a></p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
74
signup.php
Normal file
74
signup.php
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
require_once 'db/config.php';
|
||||||
|
|
||||||
|
$error = '';
|
||||||
|
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
|
$username = trim($_POST['username']);
|
||||||
|
$password = $_POST['password'];
|
||||||
|
|
||||||
|
if (empty($username) || empty($password)) {
|
||||||
|
$error = 'Please enter both username and password.';
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
$pdo = db();
|
||||||
|
// Check if username already exists
|
||||||
|
$stmt = $pdo->prepare('SELECT id FROM users WHERE username = ?');
|
||||||
|
$stmt->execute([$username]);
|
||||||
|
if ($stmt->fetch()) {
|
||||||
|
$error = 'This username is already taken.';
|
||||||
|
} else {
|
||||||
|
// Hash password and insert user
|
||||||
|
$password_hash = password_hash($password, PASSWORD_DEFAULT);
|
||||||
|
$stmt = $pdo->prepare('INSERT INTO users (username, password_hash) VALUES (?, ?)');
|
||||||
|
$stmt->execute([$username, $password_hash]);
|
||||||
|
|
||||||
|
// Log the user in and redirect
|
||||||
|
$_SESSION['user_id'] = $pdo->lastInsertId();
|
||||||
|
$_SESSION['username'] = $username;
|
||||||
|
header('Location: index.php');
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
$error = 'Database error: ' . $e->getMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Sign Up</title>
|
||||||
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||||
|
<link rel="stylesheet" href="assets/css/custom.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container d-flex justify-content-center align-items-center vh-100">
|
||||||
|
<div class="card" style="width: 24rem;">
|
||||||
|
<div class="card-body">
|
||||||
|
<h1 class="card-title text-center mb-4">Create Account</h1>
|
||||||
|
<?php if ($error): ?>
|
||||||
|
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
|
||||||
|
<?php endif; ?>
|
||||||
|
<form action="signup.php" method="POST">
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="username" class="form-label">Username</label>
|
||||||
|
<input type="text" class="form-control" id="username" name="username" required>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="password" class="form-label">Password</label>
|
||||||
|
<input type="password" class="form-control" id="password" name="password" required>
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-primary w-100">Sign Up</button>
|
||||||
|
</form>
|
||||||
|
<div class="text-center mt-3">
|
||||||
|
<p>Already have an account? <a href="login.php">Log In</a></p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Loading…
x
Reference in New Issue
Block a user