forked from iknsa-formation/jquery-spa-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.php
61 lines (46 loc) · 1.31 KB
/
login.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
<?php
session_start();
/**
* @var $_POST['email']
* @var $_POST['password']
*/
/** Si l'utilisateur est déjà connecté, retourne l'utilisateur connecté. */
if (isset($_SESSION['security'])) {
echo json_encode($_SESSION['security']);
exit;
}
/** @info Si l'email ou le mdp n'est pas renseigné retourne une erreur. */
if (!isset($_POST['email']) || !isset($_POST['password'])) {
echo json_encode([
"error" => 'You did not submit either the email or the password'
]);
exit;
}
$data = file_get_contents(__DIR__ . '/data/user.json');
$data = json_decode($data, true);
$emails = [];
foreach($data as $datum) {
$emails[] = $datum['email'];
}
/** @info Si l'email ne fait pas partie des emails acceptable, retourne une erreur */
if (!in_array($_POST['email'], $emails)) {
echo json_encode([
"error" => 'No email found in our database'
]);
exit;
}
/** @info Si le mdp n'est pas paris, retourne une erreur */
if ($_POST['password'] !== 'paris') {
echo json_encode([
"error" => 'Wrong password.'
]);
exit;
}
$data = file_get_contents(__DIR__ . '/data/user.json');
$data = json_decode($data, true);
$_SESSION['security'] = $data[$_POST["email"]];
/** @info retourne l'utilisateur connecté */
echo json_encode([
'user' => $_SESSION['security']
]);
exit;