Skip to content

Commit 289e837

Browse files
committed
The PRG Pattern and Flash old Form Data
1 parent 88095a3 commit 289e837

File tree

8 files changed

+96
-19
lines changed

8 files changed

+96
-19
lines changed

Core/Authenticator.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function attempt($email, $password)
1313

1414
])->find();
1515

16-
if (!$user) {
16+
if ($user) {
1717
if (password_verify($password, $user['password'])) {
1818
$this->login([
1919

@@ -40,12 +40,6 @@ public function login($user)
4040

4141
public function logout()
4242
{
43-
$_SESSION = [];
44-
45-
session_destroy();
46-
47-
$params = session_get_cookie_params();
48-
49-
setcookie('PHPSESSID', '', time() - 3600, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
43+
Session::destroy();
5044
}
5145
}

Core/Session.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Core;
4+
5+
class Session
6+
{
7+
public static function has($key)
8+
{
9+
return (bool) static::get($key);
10+
}
11+
12+
public static function put($key, $value)
13+
{
14+
$_SESSION[$key] = $value;
15+
}
16+
17+
public static function get($key, $default = null)
18+
{
19+
return $_SESSION['_flash'][$key] ?? $_SESSION[$key] ?? $default;
20+
}
21+
22+
public static function flash($key, $value)
23+
{
24+
$_SESSION['_flash'][$key] = $value;
25+
}
26+
27+
public static function unflash()
28+
{
29+
unset($_SESSION['_flash']);
30+
}
31+
32+
public static function flush()
33+
{
34+
$_SESSION = [];
35+
}
36+
37+
public static function destroy()
38+
{
39+
static::flush();
40+
41+
session_destroy();
42+
43+
$params = session_get_cookie_params();
44+
setcookie('PHPSESSID', '', time() - 3600, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
45+
}
46+
}

Core/functions.php

+22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
use Core\Session;
4+
25
function dd($value)
36
{
47
echo "<pre>";
@@ -55,3 +58,22 @@ function redirect($path)
5558

5659
exit();
5760
}
61+
62+
function login($user)
63+
{
64+
$_SESSION['user'] = [
65+
'email' => $user['email']
66+
];
67+
68+
session_regenerate_id(true);
69+
}
70+
71+
function logout()
72+
{
73+
Session::destroy();
74+
}
75+
76+
function old($key, $default = '')
77+
{
78+
return Core\Session::get('old')[$key] ?? $default;
79+
}

Http/controllers/session/create.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
<?php
22

3-
view('session/create.view.php');
3+
use Core\Session;
4+
5+
6+
view('session/create.view.php', [
7+
'errors' => Session::get('errors')
8+
]);

Http/controllers/session/store.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Core\Authenticator;
4+
use Core\Session;
45
use Http\Forms\LoginForm;
56

67

@@ -20,8 +21,10 @@
2021
$form->error('email', 'No matching account found for that email address and password.');
2122
}
2223

24+
Session::flash('errors', $form->errors());
25+
Session::flash('old', [
26+
'email' => $_POST['email']
27+
]);
2328

24-
return view('session/create.view.php', [
2529

26-
'errors' => $form->errors()
27-
]);
30+
return redirect('/login');

public/index.php

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Core\Session;
4+
35
session_start();
46

57

@@ -22,4 +24,7 @@
2224

2325
$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
2426
$method = $_POST['_method'] ?? $_SERVER['REQUEST_METHOD'];
27+
2528
$router->route($uri, $method);
29+
30+
Session::unflash();

views/partials/nav.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,16 @@
4747
</div>
4848

4949

50-
<div class="ml-3">
51-
<form method="post" action="/session">
50+
<?php if ($_SESSION['user'] ?? false) : ?>
51+
<div class="ml-3">
52+
<form method="post" action="/session">
5253

53-
<input type="hidden" name="_method" value="DELETE">
54+
<input type="hidden" name="_method" value="DELETE">
5455

55-
<button class="text-white">Log Out</button>
56-
</form>
57-
</div>
56+
<button class="text-white">Log Out</button>
57+
</form>
58+
</div>
59+
<?php endif; ?>
5860

5961
</div>
6062
</div>

views/session/create.view.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<div class="-space-y-px rounded-md shadow-sm">
1515
<div>
1616
<label for="email" class="sr-only">Email address</label>
17-
<input id="email" name="email" type="email" autocomplete="email" required class="relative block w-full appearance-none rounded-none rounded-t-md border border-gray-300 px-3 py-2 text-gray-900 placeholder-gray-500 focus:z-10 focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm" placeholder="Email address">
17+
<input id="email" name="email" type="email" autocomplete="email" required class="relative block w-full appearance-none rounded-none rounded-t-md border border-gray-300 px-3 py-2 text-gray-900 placeholder-gray-500 focus:z-10 focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm" placeholder="Email address" value="<?= old('email') ?>">
1818
</div>
1919

2020
<div>

0 commit comments

Comments
 (0)