Skip to content

Commit

Permalink
Add remember me to LoginAction (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
cesargb committed Mar 28, 2023
1 parent 97f42b4 commit 84320c3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
```
Expand Down
3 changes: 0 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 10 additions & 1 deletion src/Actions/LoginAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class LoginAction extends ResponseAction

protected $guard;

protected bool $remember = false;

/**
* Constructor to action.
*
Expand All @@ -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;
Expand All @@ -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();
}
Expand Down
19 changes: 19 additions & 0 deletions tests/Actions/LoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]']);
Expand Down

0 comments on commit 84320c3

Please sign in to comment.