e59-website/html/login/old/old-index2.php
2024-08-01 15:39:47 +00:00

147 lines
4.7 KiB
PHP

<?php
require("../../include/variables.php");
require("../../include/init.php");
if (isset($_SESSION['userID'])) {
header("Location: /");
die("Vous êtes déjà connecté.");
}
require("../../include/main-functions.php");
require("../../include/inputs.php");
require("../../include/panels.php");
$redirectPage = isset($_GET['p']) ? $_GET['p'] : "";
if (isset($_POST['username']) && isset($_POST['password'])) {
if (empty($_POST['username']) == false && empty($_POST['password']) == false) {
if(preg_match('!\S!u', $_POST['username']) && preg_match('!\S!u', $_POST['password'])) {
$sqlRequest = "SELECT ID, username, display_name, role, accreditation FROM users WHERE (username = :username OR email = :username) AND password = :password";
$request = $pdo->prepare($sqlRequest);
$userName = htmlspecialchars($_POST['username']);
$userPassword = md5($_POST['password']);
$request->bindParam(":username", $userName);
$request->bindParam(":password", $userPassword);
$request->execute();
$result = $request->fetchAll(PDO::FETCH_ASSOC);
if ($result) {
$_SESSION['userID'] = $result[0]['ID'];
$_SESSION['userName'] = $result[0]['username'];
$_SESSION['userDisplayName'] = $result[0]['display_name'];
$_SESSION['userRole'] = $result[0]['role'];
$_SESSION['userAccreditation'] = $result[0]['accreditation'];
header('Location: /' . $redirectPage);
exit("Login success");
} else {
$status = 'Identifiants incorrects';
}
} else {
$status = 'Caractères illégaux';
}
} else {
$status = 'Les champs ne doivent pas être vides';
}
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<?php fillHead($rootPageURL, "Se connecter - " . $pageName, $darkTheme, $lightTheme); ?>
<style>
#register-text {
font-size: 0.8em;
margin: 2em 0 2em 0;
color: #a0a0a0;
}
#register-text a {
color: var(--text);
}
.g_id_signin {
width: 248px;
border-radius: 5px;
overflow: hidden;
margin: 1em auto;
display: flex;
justify-content: center;
}
</style>
<script src="https://accounts.google.com/gsi/client" async defer></script>
<script>
function handleCredentialResponse(response) {
const data = {
id_token: response.credential
};
fetch('/login/google-auth.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => {
if (data.success) {
window.location.href = '/';
} else {
alert('Erreur d\'authentification');
}
})
.catch((error) => {
console.error('Error:', error);
});
}
</script>
</head>
<body class="body">
<header>
<div class="panel-content">
<?php fillHeader($rootPageURL, $headerTitle, $headerSubtitle, $social); ?>
</div>
</header>
<nav>
<div class="panel-content">
<?php fillNav($rootPageURL); ?>
</div>
</nav>
<main>
<div class="content">
<form action="#" method="post">
<div class="form">
<div class="form-title">Se Connecter</div>
<div class="status"><?=$status?></div>
<?php
textInput("text", "", "username", "Login / Email", "");
textInput("password", "", "password", "Mot de Passe", "");
?>
<button type="submit">Se connecter</button>
<a class="discret-button" href="google-login.php">Continuer avec Google</a>
<div id="g_id_onload" data-client_id="492430091865-vtggls1qrvs2snmeidp12gupsm964f4b.apps.googleusercontent.com" data-callback="handleCredentialResponse"></div>
<div class="g_id_signin" data-type="standard" data-shape="rectangular" data-theme="filled_black" data-text="sign_in_with" data-size="large">></div>
<div id="register-text">Vous n'avez pas de compte ? <a href="register.php?p=<?=$redirectPage?>">Créez en un</a></div>
</div>
</form>
</div>
</main>
<footer>
<div class="panel-content">
<?php fillFooter($footerText); ?>
</div>
</footer>
</body>
</html>