Skip to content

Commit 270d9fb

Browse files
committed
WIP
1 parent 0ac8b66 commit 270d9fb

9 files changed

+52
-40
lines changed

README.md

+29-10
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ class AuthServiceProvider extends ServiceProvider
9191

9292
public function boot()
9393
{
94-
Bartender::registerProvider('google');
95-
Bartender::registerProvider('microsoft');
94+
Bartender::serve('google');
95+
Bartender::serve('microsoft');
9696
}
9797
}
9898
```
@@ -111,7 +111,7 @@ class AuthServiceProvider extends ServiceProvider
111111

112112
public function boot()
113113
{
114-
Bartender::useUserModel(User::class);
114+
Bartender::setUserModel(User::class);
115115
}
116116
}
117117
```
@@ -137,13 +137,13 @@ route, and the users account will automatically be created or updated.
137137

138138
Almost everything can be swapped out in Bartender.
139139

140-
141-
If you would like to handle everything yourself for redirects and callbacks, you may create your own `ProviderHandler`:
140+
If you would like to handle everything yourself for OAuth redirects and callbacks, you may create your own `ProviderHandler`:
142141

143142
```php
144143
namespace App\Socialite;
145144

146145
use Illuminate\Http\Request;
146+
use Laravel\Socialite\Contracts\Provider;
147147
use DirectoryTree\Bartender\ProviderHandler;
148148

149149
class UserProviderHandler implements ProviderHandler
@@ -176,14 +176,32 @@ class UserProviderHandler implements ProviderHandler
176176
return redirect()->route('dashboard');
177177
}
178178
}
179-
180179
```
181180

181+
Then, provide it into the second argument in the `Bartender::serve` method:
182+
183+
```php
184+
// app/Providers/AuthServiceProvider.php
185+
186+
use App\Socialite\UserProviderHandler;
187+
use DirectoryTree\Bartender\Facades\Bartender;
188+
189+
class AuthServiceProvider extends ServiceProvider
190+
{
191+
// ...
192+
193+
public function boot()
194+
{
195+
Bartender::serve('google', UserProviderHandler::class);
196+
Bartender::serve('microsoft', UserProviderHandler::class);
197+
}
198+
}
199+
```
182200

183201
### User Creation & Updating
184202

185-
If you would like to customize the creation of the user provider, you can create your own
186-
`UserProvider` implementation:
203+
If you would like to customize the creation of the user in the default
204+
handler, you may create your own `ProviderRepository` implementation:
187205

188206
```php
189207
namespace App\Socialite;
@@ -233,8 +251,9 @@ class AppServiceProvider extends ServiceProvider
233251

234252
### User Redirects & Flash Messaging
235253

236-
If you would like to customize the behavior of the redirects and flash messages depending
237-
on the outcome of a OAuth callback, you can create your own `ProviderRedirector` implementation:
254+
If you would like to customize the behavior of the redirects of the default
255+
redirector and flash messages depending on the outcome of a OAuth callback,
256+
you can create your own `ProviderRedirector` implementation:
238257

239258
```php
240259
namespace App\Socialite;

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
],
1313
"license": "MIT",
1414
"require": {
15-
"php": ">=8.0",
15+
"php": ">=8.0",
1616
"illuminate/support": "^9.0|^10.0|^11.0",
1717
"laravel/socialite": "^5.0"
1818
},

src/BartenderManager.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,25 @@ class BartenderManager
2323
/**
2424
* Set the user model class name.
2525
*/
26-
public function useUserModel(string $userModel): void
26+
public function setUserModel(string $userModel): void
2727
{
2828
$this->userModel = $userModel;
2929
}
3030

3131
/**
3232
* Get a new user model instance.
3333
*/
34-
public function user(): Model
34+
public function getUserModel(): string
3535
{
36-
return new $this->userModel;
36+
return $this->userModel;
3737
}
3838

3939
/**
40-
* Register a new driver handler.
40+
* Register a new driver handler to serve.
4141
*
4242
* @param class-string $handler
4343
*/
44-
public function register(string $driver, string $handler = UserProviderHandler::class): void
44+
public function serve(string $driver, string $handler = UserProviderHandler::class): void
4545
{
4646
$this->handlers[$driver] = $handler;
4747
}

src/Controllers/AuthController.php

-9
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,10 @@
33
namespace DirectoryTree\Bartender\Controllers;
44

55
use Illuminate\Http\RedirectResponse;
6-
use DirectoryTree\Bartender\BartenderManager;
76
use DirectoryTree\Bartender\Facades\Bartender;
87

98
class AuthController
109
{
11-
/**
12-
* Constructor.
13-
*/
14-
public function __construct(
15-
protected BartenderManager $manager
16-
) {
17-
}
18-
1910
/**
2011
* Handle redirecting the user to the OAuth provider.
2112
*/

src/Facades/Bartender.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
/**
99
* @method static void routes()
10-
* @method static void useUserModel(string $userModel)
11-
* @method static \Illuminate\Database\Eloquent\Model user()
12-
* @method static void register(string $driver, string $handler)
10+
* @method static array handlers()
11+
* @method static string getUserModel()
12+
* @method static void setUserModel(string $userModel)
13+
* @method static void serve(string $driver, string $handler)
1314
* @method static \Illuminate\Http\RedirectResponse redirect(string $driver)
1415
* @method static \Illuminate\Http\RedirectResponse callback(string $driver)
1516
*/

src/UserProviderHandler.php

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Exception;
66
use Illuminate\Http\RedirectResponse;
7-
use Illuminate\Support\Facades\Auth;
87
use Laravel\Socialite\Contracts\Provider;
98
use Laravel\Socialite\Two\User as SocialiteUser;
109

src/UserProviderRepository.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class UserProviderRepository implements ProviderRepository
1818
*/
1919
public function exists(string $driver, SocialiteUser $user): bool
2020
{
21-
return $this->newUserQuery(Bartender::user())
21+
return $this->newUserQuery(Bartender::getUserModel())
2222
->where('email', '=', $user->email)
2323
->where(fn (Builder $query) => (
2424
$query
@@ -33,7 +33,7 @@ public function exists(string $driver, SocialiteUser $user): bool
3333
*/
3434
public function updateOrCreate(string $driver, SocialiteUser $user): Authenticatable
3535
{
36-
$model = Bartender::user();
36+
$model = Bartender::getUserModel();
3737

3838
$eloquent = $this->newUserQuery($model)->firstOrNew([
3939
'email' => $user->email,
@@ -60,28 +60,30 @@ public function updateOrCreate(string $driver, SocialiteUser $user): Authenticat
6060

6161
/**
6262
* Get a new user query instance.
63+
*
64+
* @param class-string $model
6365
*/
64-
protected function newUserQuery(Model $model): Builder
66+
protected function newUserQuery(string $model): Builder
6567
{
6668
if ($this->isUsingSoftDeletes($model)) {
67-
return $model->withTrashed();
69+
return $model::withTrashed();
6870
}
6971

70-
return $model->newQuery();
72+
return $model::query();
7173
}
7274

7375
/**
7476
* Determine if the given model uses soft deletes.
7577
*/
76-
protected function isUsingSoftDeletes(Model $model): bool
78+
protected function isUsingSoftDeletes(string $model): bool
7779
{
7880
return in_array(SoftDeletes::class, class_uses_recursive($model));
7981
}
8082

8183
/**
8284
* Determine if the given model is verifying emails.
8385
*/
84-
protected function isVerifyingEmails(Model $model): bool
86+
protected function isVerifyingEmails(string $model): bool
8587
{
8688
return in_array(MustVerifyEmail::class, class_uses_recursive($model));
8789
}

tests/BartenderManagerTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
test('it can register handlers', function () {
88
$manager = new BartenderManager();
99

10-
$manager->register('foo', stdClass::class);
10+
$manager->serve('foo', stdClass::class);
1111

1212
expect($manager->handlers())->toBe(['foo' => stdClass::class]);
1313
});
1414

1515
test('it returns new user model', function () {
1616
$manager = new BartenderManager();
1717

18-
$manager->useUserModel(User::class);
18+
$manager->setUserModel(User::class);
1919

20-
expect($manager->user())->toBeInstanceOf(User::class);
20+
expect($manager->getUserModel())->toBe(User::class);
2121
});
2222

2323
test('it is bound to facade', function () {

tests/UserProviderQueryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Illuminate\Database\UniqueConstraintViolationException;
77
use Laravel\Socialite\Two\User as SocialiteUser;
88

9-
beforeEach(fn () => Bartender::useUserModel(User::class));
9+
beforeEach(fn () => Bartender::setUserModel(User::class));
1010

1111
it('determines if user already exists with a different provider', function () {
1212
$socialite = tap(new SocialiteUser(), function ($user) {

0 commit comments

Comments
 (0)