Skip to content

Commit af521f3

Browse files
authored
Merge pull request #4861 from Laravel-Backpack/hot-fix-session-middleware-1
[hot-fix] Fix error on new Backpack installs - Target class [Backpack\CRUD\app\Http\Middleware\AuthenticateSession] does not exist.
2 parents 21b4025 + 9c057ea commit af521f3

File tree

2 files changed

+107
-115
lines changed

2 files changed

+107
-115
lines changed

src/app/Http/Middleware/AuthenticateSession.php

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,113 @@
22

33
namespace Backpack\CRUD\app\Http\Middleware;
44

5-
if (class_exists('Illuminate\Contracts\Session\Middleware\AuthenticatesSessions', false)) {
6-
class AuthenticateSession extends AuthenticateSessionL9
5+
use Closure;
6+
use Illuminate\Auth\AuthenticationException;
7+
use Illuminate\Contracts\Auth\Factory as AuthFactory;
8+
use Illuminate\Session\Middleware\AuthenticateSession as LaravelAuthenticateSession;
9+
10+
class AuthenticateSession extends LaravelAuthenticateSession
11+
{
12+
/**
13+
* The authentication factory implementation.
14+
*
15+
* @var \Illuminate\Contracts\Auth\Factory
16+
*/
17+
protected $auth;
18+
19+
protected $user;
20+
21+
/**
22+
* Create a new middleware instance.
23+
*
24+
* @param \Illuminate\Contracts\Auth\Factory $auth
25+
* @return void
26+
*/
27+
public function __construct(AuthFactory $auth)
28+
{
29+
$this->auth = $auth;
30+
$this->user = backpack_user();
31+
}
32+
33+
/**
34+
* Handle an incoming request.
35+
*
36+
* @param \Illuminate\Http\Request $request
37+
* @param \Closure $next
38+
* @return mixed
39+
*/
40+
public function handle($request, Closure $next)
41+
{
42+
if (! $request->hasSession() || ! $this->user) {
43+
return $next($request);
44+
}
45+
46+
if ($this->guard()->viaRemember()) {
47+
$passwordHash = explode('|', $request->cookies->get($this->guard()->getRecallerName()))[2] ?? null;
48+
49+
if (! $passwordHash || $passwordHash != $this->user->getAuthPassword()) {
50+
$this->logout($request);
51+
}
52+
}
53+
54+
if (! $request->session()->has('password_hash_'.backpack_guard_name())) {
55+
$this->storePasswordHashInSession($request);
56+
}
57+
58+
if ($request->session()->get('password_hash_'.backpack_guard_name()) !== $this->user->getAuthPassword()) {
59+
$this->logout($request);
60+
}
61+
62+
return tap($next($request), function () use ($request) {
63+
if (! is_null($this->guard()->user())) {
64+
$this->storePasswordHashInSession($request);
65+
}
66+
});
67+
}
68+
69+
/**
70+
* Store the user's current password hash in the session.
71+
*
72+
* @param \Illuminate\Http\Request $request
73+
* @return void
74+
*/
75+
protected function storePasswordHashInSession($request)
76+
{
77+
if (! $this->user) {
78+
return;
79+
}
80+
81+
$request->session()->put([
82+
'password_hash_'.backpack_guard_name() => $this->user->getAuthPassword(),
83+
]);
84+
}
85+
86+
/**
87+
* Log the user out of the application.
88+
*
89+
* @param \Illuminate\Http\Request $request
90+
* @return void
91+
*
92+
* @throws \Illuminate\Auth\AuthenticationException
93+
*/
94+
protected function logout($request)
95+
{
96+
$this->guard()->logoutCurrentDevice();
97+
98+
$request->session()->flush();
99+
100+
\Alert::error('Your password was changed in another browser session. Please login again using the new password.')->flash();
101+
102+
throw new AuthenticationException('Unauthenticated.', [backpack_guard_name()], backpack_url('login'));
103+
}
104+
105+
/**
106+
* Get the guard instance that should be used by the middleware.
107+
*
108+
* @return \Illuminate\Contracts\Auth\Factory|\Illuminate\Contracts\Auth\Guard
109+
*/
110+
protected function guard()
7111
{
112+
return $this->auth;
8113
}
9114
}

src/app/Http/Middleware/AuthenticateSessionL9.php

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)