From 84320c387b81682f87c1740ec789bb3143ab16d9 Mon Sep 17 00:00:00 2001 From: Cesar Garcia Date: Tue, 28 Mar 2023 20:20:55 +0200 Subject: [PATCH] Add remember me to LoginAction (#94) --- README.md | 8 +++++++- phpstan.neon.dist | 3 --- src/Actions/LoginAction.php | 11 ++++++++++- tests/Actions/LoginTest.php | 19 +++++++++++++++++++ tests/TestCase.php | 1 + 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7caed7b..605bfb1 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,13 @@ $urlShowView = MagicLink::create($action)->url; // Sample 3; Login in other guard and redirect default $action = new LoginAction(User::first()); -$action->guard('customguard')->response(redirect('/api/dashboard')); +$action->guard('customguard'); + +$urlShowView = MagicLink::create($action)->url; + +// Sample 4; Login and remember me +$action = new LoginAction(User::first()); +$action->remember(); $urlShowView = MagicLink::create($action)->url; ``` diff --git a/phpstan.neon.dist b/phpstan.neon.dist index ed01397..bd091d3 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -13,9 +13,6 @@ parameters: checkMissingIterableValueType: false ignoreErrors: - - - message: '#Parameter \#1 \$view of function view expects view-string|null, string given.#' - path: tests - message: '#Unsafe usage of new static\(\)#' path: src/MagicLink.php diff --git a/src/Actions/LoginAction.php b/src/Actions/LoginAction.php index 2529711..8658ee3 100644 --- a/src/Actions/LoginAction.php +++ b/src/Actions/LoginAction.php @@ -11,6 +11,8 @@ class LoginAction extends ResponseAction protected $guard; + protected bool $remember = false; + /** * Constructor to action. * @@ -25,6 +27,13 @@ public function __construct(Authenticatable $user, $httpResponse = null, ?string $this->guard = $guard; } + public function remember(bool $remember = true): self + { + $this->remember = $remember; + + return $this; + } + public function guard(string $guard): self { $this->guard = $guard; @@ -37,7 +46,7 @@ public function guard(string $guard): self */ public function run() { - Auth::guard($this->guard)->loginUsingId($this->authIdentifier); + Auth::guard($this->guard)->loginUsingId($this->authIdentifier, $this->remember); return parent::run(); } diff --git a/tests/Actions/LoginTest.php b/tests/Actions/LoginTest.php index 2df2a43..50c1c22 100644 --- a/tests/Actions/LoginTest.php +++ b/tests/Actions/LoginTest.php @@ -43,4 +43,23 @@ public function test_auth_custom() $this->assertAuthenticatedAs(new CustomAutenticable('user_1')); } + + public function test_auth_with_remember_me() + { + $action = new LoginAction(User::first()); + $action->remember(); + + $magiclink = MagicLink::create($action); + + $data = $this->get($magiclink->url) + ->assertStatus(302) + ->assertRedirect('/'); + + $cookieRememberMe = array_values(array_filter( + $data->headers->getCookies(), + fn($cookie) => str_starts_with($cookie->getName(),'remember_web_') + ))[0] ?? null; + + $this->assertNotNull($cookieRememberMe); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 95640d5..f35f6de 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -99,6 +99,7 @@ protected function setUpDatabase($app) $app['db']->connection()->getSchemaBuilder()->create('users', function (Blueprint $table) { $table->increments('id'); $table->string('email'); + $table->string('remember_token')->nullable(); }); User::create(['email' => 'test@user.com']);