diff --git a/src/Platform/Http/Controllers/LoginController.php b/src/Platform/Http/Controllers/LoginController.php index 4c4c7ad52..c55b87c90 100644 --- a/src/Platform/Http/Controllers/LoginController.php +++ b/src/Platform/Http/Controllers/LoginController.php @@ -57,7 +57,7 @@ public function __construct(Auth $auth) * * @return JsonResponse|RedirectResponse */ - public function login(Request $request) + public function login(Request $request, CookieJar $cookieJar) { $request->validate([ 'email' => 'required|string', @@ -66,16 +66,21 @@ public function login(Request $request) $auth = $this->guard->attempt( $request->only(['email', 'password']), - $request->filled('remember') + $request->boolean('remember') ); - if ($auth) { - return $this->sendLoginResponse($request); + if (! $auth) { + throw ValidationException::withMessages([ + 'email' => __('The details you entered did not match our records. Please double-check and try again.'), + ]); } - throw ValidationException::withMessages([ - 'email' => __('The details you entered did not match our records. Please double-check and try again.'), - ]); + if ($request->boolean('remember')) { + $user = $cookieJar->forever($this->nameForLock(), $this->guard->id()); + $cookieJar->queue($user); + } + + return $this->sendLoginResponse($request); } /** @@ -100,7 +105,7 @@ protected function sendLoginResponse(Request $request) */ public function showLoginForm(Request $request) { - $user = $request->cookie('lockUser'); + $user = $request->cookie($this->nameForLock()); /** @var EloquentUserProvider $provider */ $provider = $this->guard->getProvider(); @@ -118,7 +123,7 @@ public function showLoginForm(Request $request) */ public function resetCookieLockMe(CookieJar $cookieJar) { - $lockUser = $cookieJar->forget('lockUser'); + $lockUser = $cookieJar->forget($this->nameForLock()); return redirect()->route('platform.login')->withCookie($lockUser); } @@ -151,4 +156,14 @@ public function logout(Request $request) ? new JsonResponse([], 204) : redirect('/'); } + + /** + * Get a unique identifier for the auth session value. + * + * @return string + */ + private function nameForLock(): string + { + return sprintf('%s_%s', $this->guard->getName(), '_orchid_lock'); + } } diff --git a/src/Platform/Listeners/LockUserForLogin.php b/src/Platform/Listeners/LockUserForLogin.php deleted file mode 100644 index 17a5028bb..000000000 --- a/src/Platform/Listeners/LockUserForLogin.php +++ /dev/null @@ -1,41 +0,0 @@ -cookie = $cookieJar; - } - - /** - * Handle the event. - * - * - * @return void - */ - public function handle(Login $event) - { - if (! $event->remember) { - return; - } - - $user = $this->cookie->forever('lockUser', $event->user->id); - - $this->cookie->queue($user); - } -} diff --git a/src/Platform/Providers/EventServiceProvider.php b/src/Platform/Providers/EventServiceProvider.php deleted file mode 100644 index 255f7ee3b..000000000 --- a/src/Platform/Providers/EventServiceProvider.php +++ /dev/null @@ -1,37 +0,0 @@ - [ - LockUserForLogin::class, - ], - ]; - - /** - * Determine if events and listeners should be automatically discovered. - * - * @return bool - */ - public function shouldDiscoverEvents() - { - if (isset(static::$shouldDiscoverEvents)) { - return get_class($this) === __CLASS__ && static::$shouldDiscoverEvents === true; - } - - return parent::shouldDiscoverEvents(); - } -} diff --git a/src/Platform/Providers/FoundationServiceProvider.php b/src/Platform/Providers/FoundationServiceProvider.php index 98b79197f..a5e1a41f4 100644 --- a/src/Platform/Providers/FoundationServiceProvider.php +++ b/src/Platform/Providers/FoundationServiceProvider.php @@ -100,7 +100,6 @@ public function provides(): array IconServiceProvider::class, BreadcrumbsServiceProvider::class, RouteServiceProvider::class, - EventServiceProvider::class, PlatformServiceProvider::class, ]; } diff --git a/tests/Feature/Platform/AuthTest.php b/tests/Feature/Platform/AuthTest.php index d8d4ad97b..ce5fbb2da 100644 --- a/tests/Feature/Platform/AuthTest.php +++ b/tests/Feature/Platform/AuthTest.php @@ -4,6 +4,7 @@ namespace Orchid\Tests\Feature\Platform; +use Illuminate\Support\Facades\Auth; use Orchid\Tests\TestFeatureCase; class AuthTest extends TestFeatureCase @@ -38,7 +39,7 @@ public function testRouteDashboardLoginAuthSuccess(): void ]) ->assertStatus(302) ->assertRedirect(route(config('platform.index'))) - ->assertCookieNotExpired('lockUser'); + ->assertCookieNotExpired(sprintf('%s_%s', Auth::guard()->getName(), '_orchid_lock')); } public function testRouteDashboardLoginAuthFail(): void @@ -57,7 +58,7 @@ public function testRouteDashboardGuestLockAuth(): void 'lockUser' => 1, ]) ->assertRedirect(route('platform.login')) - ->assertCookieExpired('lockUser'); + ->assertCookieExpired(sprintf('%s_%s', Auth::guard()->getName(), '_orchid_lock')); } public function testRouteDashboardSwitchLogout(): void