Skip to content

Commit d510b17

Browse files
author
Linus Juhlin
committed
Use middleware
1 parent e32dfa8 commit d510b17

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

components/auth/app/Middleware/StartSession.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Middleware;
44

55
use Closure;
6+
use Symfony\Component\HttpFoundation\Cookie;
67

78
class StartSession
89
{
@@ -16,21 +17,31 @@ class StartSession
1617
*/
1718
public function handle($request, Closure $next, $guard = null)
1819
{
20+
$container = \App::getInstance();
21+
1922
if (session_status() == PHP_SESSION_NONE) {
2023
// In order to maintain the session between requests, we need to populate the
2124
// session ID from the supplied cookie
22-
$cookieName = \App::getInstance()['session']->getName();
25+
$cookieName = $container['session']->getName();
2326

2427
if (isset($_COOKIE[$cookieName])) {
2528
if ($sessionId = $_COOKIE[$cookieName]) {
26-
\App::getInstance()['session']->setId($sessionId);
29+
$container['session']->setId($sessionId);
2730
}
2831
}
2932

3033
// Boot the session
31-
\App::getInstance()['session']->start();
34+
$container['session']->start();
3235
}
3336

34-
return $next($request);
37+
$response = $next($request);
38+
39+
$response->headers->setCookie(new Cookie(
40+
$container['session']->getName(), $container['session']->getId(),
41+
));
42+
43+
$container['session']->save();
44+
45+
return $response;
3546
}
3647
}

components/auth/index.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161

162162
// Global middlewares
163163
$globalMiddleware = [
164+
\App\Middleware\StartSession::class,
164165
];
165166

166167
// Array middlewares
@@ -180,19 +181,6 @@
180181
// Create a request from server variables
181182
$request = Request::capture();
182183

183-
// In order to maintain the session between requests, we need to populate the
184-
// session ID from the supplied cookie
185-
$cookieName = $container['session']->getName();
186-
187-
if (isset($_COOKIE[$cookieName])) {
188-
if ($sessionId = $_COOKIE[$cookieName]) {
189-
$container['session']->setId($sessionId);
190-
}
191-
}
192-
193-
// Boot the session
194-
$container['session']->start();
195-
196184
// Dispatching the request:
197185
// When it comes to dispatching the request, you have two options:
198186
// a) you either send the request directly through the router
@@ -210,12 +198,6 @@
210198
return $router->dispatch($request);
211199
});
212200

213-
$response->headers->setCookie(new Cookie(
214-
$container['session']->getName(), $container['session']->getId(),
215-
));
216-
217-
$container['session']->save();
218-
219201
// Send the response back to the browser
220202
$response->send();
221203

0 commit comments

Comments
 (0)