Delete comment
This commit is contained in:
@@ -1,114 +1,107 @@
|
||||
// check_auth.js
|
||||
$(document).ready(function() {
|
||||
// Проверка авторизации при загрузке страницы
|
||||
checkAuthStatus();
|
||||
|
||||
// Обработка формы входа
|
||||
$('#loginForm').on('submit', function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
const email = $('#login-email').val();
|
||||
const password = $('#login-password').val();
|
||||
const remember = $('#remember').is(':checked');
|
||||
|
||||
$.ajax({
|
||||
url: 'login_handler.php',
|
||||
method: 'POST',
|
||||
data: {
|
||||
email: email,
|
||||
password: password
|
||||
},
|
||||
success: function(response) {
|
||||
try {
|
||||
const result = JSON.parse(response);
|
||||
if (result.success) {
|
||||
// Сохраняем в localStorage если выбрано "Запомнить меня"
|
||||
if (remember) {
|
||||
localStorage.setItem('rememberedEmail', email);
|
||||
} else {
|
||||
localStorage.removeItem('rememberedEmail');
|
||||
}
|
||||
|
||||
// Перенаправляем
|
||||
window.location.href = result.redirect || 'catalog.php';
|
||||
} else {
|
||||
showMessage('error', result.message || 'Ошибка авторизации');
|
||||
}
|
||||
} catch(e) {
|
||||
showMessage('error', 'Ошибка обработки ответа');
|
||||
}
|
||||
},
|
||||
error: function() {
|
||||
showMessage('error', 'Ошибка сервера');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Проверка статуса авторизации
|
||||
function checkAuthStatus() {
|
||||
$.ajax({
|
||||
url: 'check_auth_status.php',
|
||||
method: 'GET',
|
||||
success: function(response) {
|
||||
try {
|
||||
const result = JSON.parse(response);
|
||||
if (result.loggedIn) {
|
||||
updateUserProfile(result.user);
|
||||
}
|
||||
} catch(e) {
|
||||
console.error('Ошибка проверки авторизации', e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Обновление профиля пользователя
|
||||
function updateUserProfile(user) {
|
||||
// Обновляем шапку, если есть элементы для профиля
|
||||
if ($('#userEmail').length) {
|
||||
$('#userEmail').text(user.email);
|
||||
}
|
||||
if ($('#userName').length) {
|
||||
$('#userName').text(user.full_name);
|
||||
}
|
||||
}
|
||||
|
||||
// Показать сообщение
|
||||
function showMessage(type, text) {
|
||||
const $message = $('#' + type + 'Message');
|
||||
if ($message.length) {
|
||||
$message.text(text).fadeIn();
|
||||
setTimeout(() => $message.fadeOut(), 5000);
|
||||
} else {
|
||||
alert(text);
|
||||
}
|
||||
}
|
||||
|
||||
// Проверка авторизации для ссылок
|
||||
function checkAuth(redirectUrl) {
|
||||
$.ajax({
|
||||
url: 'check_auth_status.php',
|
||||
method: 'GET',
|
||||
success: function(response) {
|
||||
try {
|
||||
const result = JSON.parse(response);
|
||||
if (result.loggedIn) {
|
||||
window.location.href = redirectUrl;
|
||||
} else {
|
||||
// Показываем модальное окно или перенаправляем на вход
|
||||
showLoginModal(redirectUrl);
|
||||
}
|
||||
} catch(e) {
|
||||
showLoginModal(redirectUrl);
|
||||
}
|
||||
}
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
// Показать модальное окно входа
|
||||
function showLoginModal(redirectUrl) {
|
||||
// Можно реализовать модальное окно или перенаправить на страницу входа
|
||||
window.location.href = 'вход.php?redirect=' + encodeURIComponent(redirectUrl);
|
||||
}
|
||||
});
|
||||
|
||||
$(document).ready(function() {
|
||||
|
||||
checkAuthStatus();
|
||||
|
||||
$('#loginForm').on('submit', function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
const email = $('#login-email').val();
|
||||
const password = $('#login-password').val();
|
||||
const remember = $('#remember').is(':checked');
|
||||
|
||||
$.ajax({
|
||||
url: 'login_handler.php',
|
||||
method: 'POST',
|
||||
data: {
|
||||
email: email,
|
||||
password: password
|
||||
},
|
||||
success: function(response) {
|
||||
try {
|
||||
const result = JSON.parse(response);
|
||||
if (result.success) {
|
||||
|
||||
if (remember) {
|
||||
localStorage.setItem('rememberedEmail', email);
|
||||
} else {
|
||||
localStorage.removeItem('rememberedEmail');
|
||||
}
|
||||
|
||||
window.location.href = result.redirect || 'catalog.php';
|
||||
} else {
|
||||
showMessage('error', result.message || 'Ошибка авторизации');
|
||||
}
|
||||
} catch(e) {
|
||||
showMessage('error', 'Ошибка обработки ответа');
|
||||
}
|
||||
},
|
||||
error: function() {
|
||||
showMessage('error', 'Ошибка сервера');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function checkAuthStatus() {
|
||||
$.ajax({
|
||||
url: 'check_auth_status.php',
|
||||
method: 'GET',
|
||||
success: function(response) {
|
||||
try {
|
||||
const result = JSON.parse(response);
|
||||
if (result.loggedIn) {
|
||||
updateUserProfile(result.user);
|
||||
}
|
||||
} catch(e) {
|
||||
console.error('Ошибка проверки авторизации', e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function updateUserProfile(user) {
|
||||
|
||||
if ($('#userEmail').length) {
|
||||
$('#userEmail').text(user.email);
|
||||
}
|
||||
if ($('#userName').length) {
|
||||
$('#userName').text(user.full_name);
|
||||
}
|
||||
}
|
||||
|
||||
function showMessage(type, text) {
|
||||
const $message = $('#' + type + 'Message');
|
||||
if ($message.length) {
|
||||
$message.text(text).fadeIn();
|
||||
setTimeout(() => $message.fadeOut(), 5000);
|
||||
} else {
|
||||
alert(text);
|
||||
}
|
||||
}
|
||||
|
||||
function checkAuth(redirectUrl) {
|
||||
$.ajax({
|
||||
url: 'check_auth_status.php',
|
||||
method: 'GET',
|
||||
success: function(response) {
|
||||
try {
|
||||
const result = JSON.parse(response);
|
||||
if (result.loggedIn) {
|
||||
window.location.href = redirectUrl;
|
||||
} else {
|
||||
|
||||
showLoginModal(redirectUrl);
|
||||
}
|
||||
} catch(e) {
|
||||
showLoginModal(redirectUrl);
|
||||
}
|
||||
}
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
function showLoginModal(redirectUrl) {
|
||||
|
||||
window.location.href = 'вход.php?redirect=' + encodeURIComponent(redirectUrl);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
<?php
|
||||
// config/database.php
|
||||
class Database {
|
||||
private static $instance = null;
|
||||
private $connection;
|
||||
|
||||
private function __construct() {
|
||||
try {
|
||||
$this->connection = new PDO(
|
||||
"pgsql:host=185.130.224.177;port=5481;dbname=postgres",
|
||||
"admin",
|
||||
"38feaad2840ccfda0e71243a6faaecfd"
|
||||
);
|
||||
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$this->connection->exec("SET NAMES 'utf8'");
|
||||
} catch(PDOException $e) {
|
||||
die("Ошибка подключения: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static function getInstance() {
|
||||
if (self::$instance == null) {
|
||||
self::$instance = new Database();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function getConnection() {
|
||||
return $this->connection;
|
||||
}
|
||||
}
|
||||
<?php
|
||||
|
||||
class Database {
|
||||
private static $instance = null;
|
||||
private $connection;
|
||||
|
||||
private function __construct() {
|
||||
try {
|
||||
$this->connection = new PDO(
|
||||
"pgsql:host=185.130.224.177;port=5481;dbname=postgres",
|
||||
"admin",
|
||||
"38feaad2840ccfda0e71243a6faaecfd"
|
||||
);
|
||||
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$this->connection->exec("SET NAMES 'utf8'");
|
||||
} catch(PDOException $e) {
|
||||
die("Ошибка подключения: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static function getInstance() {
|
||||
if (self::$instance == null) {
|
||||
self::$instance = new Database();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function getConnection() {
|
||||
return $this->connection;
|
||||
}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user