-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogout.php
68 lines (58 loc) · 2.11 KB
/
logout.php
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
<?php
require 'vendor/autoload.php';
use \Firebase\JWT\JWT;
use Firebase\JWT\Key;
$config = include 'config.php';
header('Content-Type: application/json');
function getTokenFromRequest() {
$headers = apache_request_headers();
if (isset($headers['Authorization'])) {
return str_replace('Bearer ', '', $headers['Authorization']);
}
return null;
}
function decodeJwt($jwt, $secretKey, $encoder_type)
{
try{
return JWT::decode($jwt, new Key($secretKey, $encoder_type));
} catch (Exception $e) {
return null;
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$token = getTokenFromRequest();
if (!$token) {
http_response_code(400);
echo json_encode(["error" => "Где токен?"]);
exit;
}
$decoded = decodeJwt($token, $config['secret_key'], $config['encoder_type']);
if (!$decoded) {
http_response_code(401);
echo json_encode(["error" => "Неверный токен."]);
exit;
}
$userGuid = $decoded->userGuid;
try {
$pdo = new PDO(
"mysql:host={$config['db_host']};dbname={$config['db_name']};charset={$config['db_charset']}",
$config['db_user'],
$config['db_password']
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare("UPDATE users SET token = '0' WHERE userGuid = ?");
$stmt->execute([$userGuid]);
if ($stmt->rowCount() > 0) {
echo json_encode(["message" => "Вы успешно вышли из системы."]);
} else {
http_response_code(400);
echo json_encode(["error" => "Не удалось выйти. Пользователь не найден."]);
}
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(["error" => "Ошибка подключения к базе данных: " . $e->getMessage()]);
}
} else {
echo json_encode(["message" => "Метод не поддерживается (нужен POST)"]);
}
?>