Skip to content

Commit

Permalink
Merge pull request #59 from cesargb/user_store
Browse files Browse the repository at this point in the history
LoginAction: only store auth identifier instead of user class
  • Loading branch information
cesargb committed Jan 8, 2022
2 parents 7314bca + f68490c commit 51cd3fd
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/Actions/LoginAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

class LoginAction extends ResponseAction
{
protected $user;
protected $authIdentifier;

protected $guard;

/**
* Constructor to action.
*
* @param mixed $httpResponse
* @param string|null $guard
* @param string $guard
*/
public function __construct(Authenticatable $user, $httpResponse = null, string $guard = 'web')
{
$this->user = $user;
$this->authIdentifier = $user->getAuthIdentifier();

$this->httpResponse = $this->serializeResponse($httpResponse);

Expand All @@ -31,7 +31,7 @@ public function __construct(Authenticatable $user, $httpResponse = null, string
*/
public function run()
{
Auth::guard($this->guard)->login($this->user);
Auth::guard($this->guard)->loginUsingId($this->authIdentifier);

return parent::run();
}
Expand Down
26 changes: 25 additions & 1 deletion tests/Actions/LoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace MagicLink\Test\Actions;

use Illuminate\Support\Facades\Auth;
use MagicLink\Actions\LoginAction;
use MagicLink\MagicLink;
use MagicLink\Test\TestCase;
use MagicLink\Test\User;
use MagicLink\Test\TestSupport\CustomAutenticable;
use MagicLink\Test\TestSupport\CustomUserProvider;
use MagicLink\Test\TestSupport\User;

class LoginTest extends TestCase
{
Expand All @@ -19,4 +22,25 @@ public function test_auth()

$this->assertAuthenticatedAs(User::first());
}

public function test_auth_custom()
{
Auth::provider('custom_provider', function ($app, array $config) {
return new CustomUserProvider($app, $config);
});

config()->set('auth.providers.custom', [
'driver' => 'custom_provider',
]);

config()->set('auth.guards.web.provider', 'custom');

$magiclink = MagicLink::create(new LoginAction(new CustomAutenticable('user_1')));

$this->get($magiclink->url)
->assertStatus(302)
->assertRedirect('/');

$this->assertAuthenticatedAs(new CustomAutenticable('user_1'));
}
}
2 changes: 1 addition & 1 deletion tests/Actions/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use MagicLink\Actions\ResponseAction;
use MagicLink\MagicLink;
use MagicLink\Test\TestCase;
use MagicLink\Test\User;
use MagicLink\Test\TestSupport\User;

class ResponseTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Actions/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use MagicLink\Actions\ViewAction;
use MagicLink\MagicLink;
use MagicLink\Test\TestCase;
use MagicLink\Test\User;
use MagicLink\Test\TestSupport\User;

class ViewTest extends TestCase
{
Expand Down
1 change: 1 addition & 0 deletions tests/MagicLinkDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use MagicLink\Actions\LoginAction;
use MagicLink\MagicLink;
use MagicLink\Test\TestSupport\User;

class MagicLinkDeleteTest extends TestCase
{
Expand Down
1 change: 1 addition & 0 deletions tests/MagicLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use MagicLink\Actions\LoginAction;
use MagicLink\MagicLink;
use MagicLink\Test\TestSupport\User;

class MagicLinkTest extends TestCase
{
Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Schema\Blueprint;
use MagicLink\MagicLinkServiceProvider;
use MagicLink\Test\TestSupport\User;
use Orchestra\Testbench\TestCase as Orchestra;

abstract class TestCase extends Orchestra
Expand Down Expand Up @@ -35,7 +36,7 @@ protected function getEnvironmentSetUp($app)
{
$app['config']->set('app.key', 'base64:mJlbzP1TMXUPouK3KK6e9zS/VvxtWTfzfVlkn1JTqpM=');

$app['config']->set('auth.providers.users.model', 'MagicLink\Test\User');
$app['config']->set('auth.providers.users.model', 'MagicLink\Test\TestSupport\User');

$app['config']->set('view.paths', [__DIR__.'/stubs/resources/views']);

Expand Down
40 changes: 40 additions & 0 deletions tests/TestSupport/CustomAutenticable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace MagicLink\Test\TestSupport;

use Illuminate\Contracts\Auth\Authenticatable;

class CustomAutenticable implements Authenticatable
{
private $id;

public function __construct($id)
{
$this->id = $id;
}

public function getAuthIdentifierName()
{
}

public function getAuthIdentifier()
{
return $this->id;
}

public function getAuthPassword()
{
}

public function getRememberToken()
{
}

public function setRememberToken($value)
{
}

public function getRememberTokenName()
{
}
}
36 changes: 36 additions & 0 deletions tests/TestSupport/CustomUserProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace MagicLink\Test\TestSupport;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;

class CustomUserProvider implements UserProvider
{
public function retrieveById($identifier)
{
return new CustomAutenticable($identifier);
}

public function retrieveByToken($identifier, $token)
{
print_r([__LINE__ => $identifier]);
}

public function updateRememberToken(Authenticatable $user, $token)
{
print_r([__LINE__ => $user]);
}

public function retrieveByCredentials(array $credentials)
{
print_r([__LINE__ => $credentials]);

//return User::where('username', $username)->first();
}

public function validateCredentials(Authenticatable $user, array $credentials)
{
print_r([__LINE__ => $credentials]);
}
}
2 changes: 1 addition & 1 deletion tests/User.php → tests/TestSupport/User.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MagicLink\Test;
namespace MagicLink\Test\TestSupport;

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
Expand Down
2 changes: 1 addition & 1 deletion tests/stubs/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use MagicLink\Actions\LoginAction;
use MagicLink\Actions\ResponseAction;
use MagicLink\MagicLink;
use MagicLink\Test\User;
use MagicLink\Test\TestSupport\User;

Route::get('/create/login', function () {
return MagicLink::create(new LoginAction(User::first()))->url;
Expand Down

0 comments on commit 51cd3fd

Please sign in to comment.