feat(login/register) blabla

main
Valentin 2024-06-10 18:19:34 +02:00
parent f6570e074c
commit 10825d7598
17 changed files with 243 additions and 38 deletions

49
assets/css/login.css Normal file
View File

@ -0,0 +1,49 @@
html,
body {
height: 100%;
}
body {
display: -ms-flexbox;
display: -webkit-box;
display: flex;
-ms-flex-align: center;
-ms-flex-pack: center;
-webkit-box-align: center;
align-items: center;
-webkit-box-pack: center;
justify-content: center;
padding-top: 40px;
padding-bottom: 40px;
background-color: #f5f5f5;
}
.form-signin {
width: 100%;
max-width: 330px;
padding: 15px;
margin: 0 auto;
}
.form-signin .checkbox {
font-weight: 400;
}
.form-signin .form-control {
position: relative;
box-sizing: border-box;
height: auto;
padding: 10px;
font-size: 16px;
}
.form-signin .form-control:focus {
z-index: 2;
}
.form-signin input[type="email"] {
margin-bottom: -1px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}
.form-signin input[type="password"] {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
}

BIN
assets/logo/dark.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

BIN
assets/logo/purple.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

View File

@ -1,9 +1,10 @@
<?php
$line = '2';
include 'components/homepage/line.php';
?>
<hr>
<?php
$line = '5';
$line = '8';
include 'components/homepage/line.php';
?>

33
components/login/main.php Normal file
View File

@ -0,0 +1,33 @@
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$query = $conn->prepare("SELECT * FROM users WHERE email = :email");
$query->bindParam(':email', $_POST['email']);
$query->execute();
$user = $query->fetch(PDO::FETCH_ASSOC);
if ($user && password_verify($_POST['password'], $user['password'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['first_name'] = $user['first_name'];
$_SESSION['is_admin'] = $user['is_admin'];
header("Location: index.php");
} else {
$errorMessage = "Invalid email or password.";
}
}
?>
<body class="text-center">
<form class="form-signin" method="POST" action="login.php">
<img class="mb-4" src="assets/logo/dark.png" alt="" width="256px">
<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
<?= isset($errorMessage) ? '<div class="alert alert-danger" role="alert">' . $errorMessage . '</div>' : '' ?>
<input type="email" id="email" name="email" class="form-control mt-2" placeholder="damien.dupuis@gmail.com" required autofocus>
<input type="password" id="password" name="password" class="form-control mt-2" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Continue</button>
<p class="mt-5 mb-3 text-muted">Not registered ? <a class="text-decoration-none" href="/register.php">Create an account here</a></p>
</form>
</body>

View File

@ -1,5 +1,14 @@
<?php
include 'services/stop_service.php';
function getStops($line) {
$json = file_get_contents(__DIR__ . '/../../data/stops.json');
$data = json_decode($json, true);
$result = array_filter($data, function($item) use ($line) {
return $item['fields']['mode'] === 'METRO' && $item['fields']['indice_lig'] === "$line";
});
return $result;
}
?>
<h2>Lignes de Métro</h2>
@ -20,7 +29,6 @@ include 'services/stop_service.php';
</div>
<div class="modal-body">
<?php
$stops = getStops($i);
include 'components/navigate/stop_list.php';
?>
</div>

View File

@ -1,6 +1,8 @@
<div class="row">
<?php
$stations = array_fill(1, 10, 'Station');
$stations = getStops($i);
$half = ceil(count($stations) / 2);
$chunks = array_chunk($stations, ceil(count($stations) / 2), true);
?>
@ -17,12 +19,12 @@
<tbody>
<?php foreach ($chunk as $station): ?>
<tr>
<td><?php echo $station; ?></td>
<td class="text-end"><div class="btn btn-success">Ajouter</div></td>
<td><?php echo htmlspecialchars($station['fields']['nom_zda']); ?></td>
<td class="text-end"><button class="btn btn-success">Ajouter</button></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endforeach; ?>
</div>
</div>

View File

@ -0,0 +1,50 @@
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST['password'] !== $_POST['confirmPassword']) {
$errorMessage = "Password doesnt match";
} else {
$query = $conn->prepare("SELECT * FROM users WHERE email = :email");
$query->bindParam(':email', $_POST['email']);
$query->execute();
$user = $query->fetch(PDO::FETCH_ASSOC);
if ($user) {
$errorMessage = "Email already used.";
}
}
}
?>
<body class="text-center">
<form class="form-signin" method="POST" action="register.php">
<img src="assets/logo/dark.png" alt="" width="256px">
<h1 class="h3 mb-3 font-weight-normal">Create an account</h1>
<?= isset($errorMessage) ? '<div class="alert alert-danger" role="alert">' . $errorMessage . '</div>' : '' ?>
<div class="row">
<div class="col">
<input type="text" id="firstName" name="firstName" class="form-control mt-2" placeholder="Damien" required>
</div>
<div class="col">
<input type="text" id="lastName" name="lastName" class="form-control mt-2" placeholder="Dupuis" required>
</div>
</div>
<input type="email" id="email" name="email" class="form-control mt-2" placeholder="damien.dupuis@gmail.com" required>
<hr>
<div class="row">
<div class="col">
<input type="password" id="password" name="password" class="form-control" placeholder="Password" required>
</div>
<div class="col">
<input type="password" id="confirmPassword" name="confirmPassword" class="form-control" placeholder="Confirmation" required>
</div>
</div>
<button class="btn btn-lg btn-primary btn-block mt-4" type="submit">Continue</button>
<p class="mt-5 mb-3 text-muted">Already an account ? <a class="text-decoration-none" href="/login.php">Login here</a></p>
</form>
</body>

View File

@ -1,4 +1,5 @@
<?php
$content = 'components/homepage/main.php';
include 'structure/main.php';
?>

4
login.php Normal file
View File

@ -0,0 +1,4 @@
<?php
$content = 'components/login/main.php';
include 'structure/main.php';
?>

7
logout.php Normal file
View File

@ -0,0 +1,7 @@
<?php
session_start();
session_unset();
session_destroy();
header("Location: login.php");
exit();
?>

4
register.php Normal file
View File

@ -0,0 +1,4 @@
<?php
$content = 'components/register/main.php';
include 'structure/main.php';
?>

22
services/db.php Normal file
View File

@ -0,0 +1,22 @@
<?php
try {
$conn = new PDO("mysql:host=127.0.0.1", "root", "lynqo");
$conn->exec("CREATE DATABASE IF NOT EXISTS subwaySchedule");
$conn->exec("USE subwaySchedule");
$conn->exec("CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
firstName VARCHAR(50) NOT NULL,
lastName VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
is_admin BOOLEAN NOT NULL DEFAULT 0
)");
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>

View File

@ -1,10 +1,5 @@
<?php
function getStops($line) {
$json = file_get_contents("../data/stops.json");
$data = json_decode($json, true);
$result = array_filter($data, function($item) use ($line) {
return $item['fields']['mode'] === 'METRO' && $item['fields']['indice_lig'] === "$line";
});
return $result;
}
?>

View File

@ -5,6 +5,10 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Subway schedules</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<?php
if (!isset($_SESSION['user_id'])) {
echo '<link href="assets/css/login.css" rel="stylesheet">';
}
?>
</head>
<body>

View File

@ -1,5 +1,20 @@
<?php include 'structure/header.php'; ?>
<?php include 'structure/navbar.php'; ?>
<?php
session_start();
$page = basename($_SERVER['PHP_SELF']);
if (!isset($_SESSION['user_id']) && $page !== 'login.php' && $page !== 'register.php') {
header("Location: login.php");
exit();
} else if (isset($_SESSION['user_id']) && ($page === 'login.php' || $page === 'register.php')) {
header("Location: index.php");
exit();
}
include 'structure/header.php';
include 'structure/navbar.php';
include 'services/db.php';
?>
<main class="container mt-5">
<?php include $content; ?>

View File

@ -1,25 +1,35 @@
<header>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<?php
if (isset($_SESSION['user_id'])) {
echo '
<header>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Subways</a>
<a class="navbar-brand" href="/">Subways</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="/">Accueil</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/navigate.php">Parcourir</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Mon compte</a>
</li>
</ul>
<div class="ms-auto">
<a class="btn btn-primary" href="#">Gestion des utilisateurs</a>
</div>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="/">Accueil</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/navigate.php">Parcourir</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Mon compte</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/logout.php">Déconnexion</a>
</li>
</ul>
<div class="ms-auto">
<a class="btn btn-primary" href="#">Gestion des utilisateurs</a>
</div>
</nav>
</header>
</div>
</div>
</nav>
</header>';
}
?>