Skip to content

Commit 51cd3fd

Browse files
authored
Merge pull request #59 from cesargb/user_store
LoginAction: only store auth identifier instead of user class
2 parents 7314bca + f68490c commit 51cd3fd

File tree

11 files changed

+113
-10
lines changed

11 files changed

+113
-10
lines changed

src/Actions/LoginAction.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77

88
class LoginAction extends ResponseAction
99
{
10-
protected $user;
10+
protected $authIdentifier;
1111

1212
protected $guard;
1313

1414
/**
1515
* Constructor to action.
1616
*
1717
* @param mixed $httpResponse
18-
* @param string|null $guard
18+
* @param string $guard
1919
*/
2020
public function __construct(Authenticatable $user, $httpResponse = null, string $guard = 'web')
2121
{
22-
$this->user = $user;
22+
$this->authIdentifier = $user->getAuthIdentifier();
2323

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

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

3636
return parent::run();
3737
}

tests/Actions/LoginTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace MagicLink\Test\Actions;
44

5+
use Illuminate\Support\Facades\Auth;
56
use MagicLink\Actions\LoginAction;
67
use MagicLink\MagicLink;
78
use MagicLink\Test\TestCase;
8-
use MagicLink\Test\User;
9+
use MagicLink\Test\TestSupport\CustomAutenticable;
10+
use MagicLink\Test\TestSupport\CustomUserProvider;
11+
use MagicLink\Test\TestSupport\User;
912

1013
class LoginTest extends TestCase
1114
{
@@ -19,4 +22,25 @@ public function test_auth()
1922

2023
$this->assertAuthenticatedAs(User::first());
2124
}
25+
26+
public function test_auth_custom()
27+
{
28+
Auth::provider('custom_provider', function ($app, array $config) {
29+
return new CustomUserProvider($app, $config);
30+
});
31+
32+
config()->set('auth.providers.custom', [
33+
'driver' => 'custom_provider',
34+
]);
35+
36+
config()->set('auth.guards.web.provider', 'custom');
37+
38+
$magiclink = MagicLink::create(new LoginAction(new CustomAutenticable('user_1')));
39+
40+
$this->get($magiclink->url)
41+
->assertStatus(302)
42+
->assertRedirect('/');
43+
44+
$this->assertAuthenticatedAs(new CustomAutenticable('user_1'));
45+
}
2246
}

tests/Actions/ResponseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use MagicLink\Actions\ResponseAction;
88
use MagicLink\MagicLink;
99
use MagicLink\Test\TestCase;
10-
use MagicLink\Test\User;
10+
use MagicLink\Test\TestSupport\User;
1111

1212
class ResponseTest extends TestCase
1313
{

tests/Actions/ViewTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use MagicLink\Actions\ViewAction;
66
use MagicLink\MagicLink;
77
use MagicLink\Test\TestCase;
8-
use MagicLink\Test\User;
8+
use MagicLink\Test\TestSupport\User;
99

1010
class ViewTest extends TestCase
1111
{

tests/MagicLinkDeleteTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use MagicLink\Actions\LoginAction;
66
use MagicLink\MagicLink;
7+
use MagicLink\Test\TestSupport\User;
78

89
class MagicLinkDeleteTest extends TestCase
910
{

tests/MagicLinkTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use MagicLink\Actions\LoginAction;
66
use MagicLink\MagicLink;
7+
use MagicLink\Test\TestSupport\User;
78

89
class MagicLinkTest extends TestCase
910
{

tests/TestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Schema\Blueprint;
66
use MagicLink\MagicLinkServiceProvider;
7+
use MagicLink\Test\TestSupport\User;
78
use Orchestra\Testbench\TestCase as Orchestra;
89

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

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

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace MagicLink\Test\TestSupport;
4+
5+
use Illuminate\Contracts\Auth\Authenticatable;
6+
7+
class CustomAutenticable implements Authenticatable
8+
{
9+
private $id;
10+
11+
public function __construct($id)
12+
{
13+
$this->id = $id;
14+
}
15+
16+
public function getAuthIdentifierName()
17+
{
18+
}
19+
20+
public function getAuthIdentifier()
21+
{
22+
return $this->id;
23+
}
24+
25+
public function getAuthPassword()
26+
{
27+
}
28+
29+
public function getRememberToken()
30+
{
31+
}
32+
33+
public function setRememberToken($value)
34+
{
35+
}
36+
37+
public function getRememberTokenName()
38+
{
39+
}
40+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace MagicLink\Test\TestSupport;
4+
5+
use Illuminate\Contracts\Auth\Authenticatable;
6+
use Illuminate\Contracts\Auth\UserProvider;
7+
8+
class CustomUserProvider implements UserProvider
9+
{
10+
public function retrieveById($identifier)
11+
{
12+
return new CustomAutenticable($identifier);
13+
}
14+
15+
public function retrieveByToken($identifier, $token)
16+
{
17+
print_r([__LINE__ => $identifier]);
18+
}
19+
20+
public function updateRememberToken(Authenticatable $user, $token)
21+
{
22+
print_r([__LINE__ => $user]);
23+
}
24+
25+
public function retrieveByCredentials(array $credentials)
26+
{
27+
print_r([__LINE__ => $credentials]);
28+
29+
//return User::where('username', $username)->first();
30+
}
31+
32+
public function validateCredentials(Authenticatable $user, array $credentials)
33+
{
34+
print_r([__LINE__ => $credentials]);
35+
}
36+
}

tests/User.php renamed to tests/TestSupport/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace MagicLink\Test;
3+
namespace MagicLink\Test\TestSupport;
44

55
use Illuminate\Auth\Authenticatable;
66
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;

0 commit comments

Comments
 (0)