-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.php
More file actions
120 lines (99 loc) · 3.55 KB
/
auth.php
File metadata and controls
120 lines (99 loc) · 3.55 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
<?php
require_once 'config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action'] ?? '';
switch ($action) {
case 'login':
handleLogin();
break;
case 'register':
handleRegister();
break;
case 'logout':
handleLogout();
break;
default:
sendResponse(false, 'Invalid action');
}
}
function handleLogin() {
global $pdo;
$email = trim($_POST['email'] ?? '');
$password = $_POST['password'] ?? '';
if (empty($email) || empty($password)) {
sendResponse(false, 'Email and password are required');
}
try {
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?");
$stmt->execute([$email]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['user_name'] = $user['name'];
$_SESSION['user_email'] = $user['email'];
$_SESSION['user_role'] = $user['role'];
sendResponse(true, 'Login successful', [
'id' => $user['id'], // Include ID for completeness
'name' => $user['name'],
'email' => $user['email'],
'role' => $user['role'] ?? 'content_manager' // Default role if missing
]);
} else {
sendResponse(false, 'Invalid email or password');
}
} catch(PDOException $e) {
sendResponse(false, 'Database error: ' . $e->getMessage());
}
}
function handleRegister() {
global $pdo;
$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
$password = $_POST['password'] ?? '';
$confirmPassword = $_POST['confirm_password'] ?? '';
// Validation
if (empty($name) || empty($email) || empty($password)) {
sendResponse(false, 'All fields are required');
}
if ($password !== $confirmPassword) {
sendResponse(false, 'Passwords do not match');
}
if (strlen($password) < 6) {
sendResponse(false, 'Password must be at least 6 characters long');
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
sendResponse(false, 'Invalid email format');
}
try {
// Check if email already exists
$stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
$stmt->execute([$email]);
if ($stmt->fetch()) {
sendResponse(false, 'Email already registered');
}
// Hash password and insert user
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("INSERT INTO users (name, email, password, role) VALUES (?, ?, ?, 'content_manager')");
$stmt->execute([$name, $email, $hashedPassword]);
sendResponse(true, 'Account created successfully! Please login.');
} catch(PDOException $e) {
sendResponse(false, 'Database error: ' . $e->getMessage());
}
}
function handleLogout() {
session_destroy();
sendResponse(true, 'Logged out successfully');
}
// Check login status endpoint
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['check_auth'])) {
if (isLoggedIn()) {
sendResponse(true, 'User is logged in', [
'name' => $_SESSION['user_name'],
'email' => $_SESSION['user_email'],
'role' => $_SESSION['user_role']
]);
} else {
sendResponse(false, 'User not logged in');
}
}
?>