29 lines
915 B
PHP
29 lines
915 B
PHP
<?php
|
|
session_start();
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/../database/db_connect.php';
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$username = trim($input['login'] ?? '');
|
|
$password = $input['password'] ?? '';
|
|
|
|
if (!$username || !$password) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Username and password required']);
|
|
exit;
|
|
}
|
|
|
|
$stmt = $pdo->prepare("SELECT id, username, password FROM users WHERE username = ?");
|
|
$stmt->execute([$username]);
|
|
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($user && password_verify($password, $user['password'])) {
|
|
$_SESSION['user_id'] = $user['id'];
|
|
$_SESSION['username'] = $user['username'];
|
|
echo json_encode(['ok' => true, 'user' => ['id' => $user['id'], 'username' => $user['username']]]);
|
|
} else {
|
|
http_response_code(401);
|
|
echo json_encode(['error' => 'Invalid credentials']);
|
|
}
|
|
?>
|