35 lines
1.1 KiB
PHP
35 lines
1.1 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/auth_helper.php';
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$email = $data['email'] ?? '';
|
|
$password = $data['password'] ?? '';
|
|
|
|
if ($email === '' || $password === '') {
|
|
echo json_encode(['success' => false, 'error' => 'Email and password are required.']);
|
|
exit;
|
|
}
|
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
echo json_encode(['success' => false, 'error' => 'Invalid email format.']);
|
|
exit;
|
|
}
|
|
|
|
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
|
|
|
|
try {
|
|
$stmt = db()->prepare("INSERT INTO users (email, password_hash) VALUES (?, ?)");
|
|
if ($stmt->execute([$email, $hashedPassword])) {
|
|
$_SESSION['user_id'] = db()->lastInsertId();
|
|
$_SESSION['email'] = $email;
|
|
echo json_encode(['success' => true]);
|
|
}
|
|
} catch (PDOException $e) {
|
|
if ($e->getCode() == 23000) {
|
|
echo json_encode(['success' => false, 'error' => 'Email is already registered.']);
|
|
} else {
|
|
echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]);
|
|
}
|
|
}
|