19
19
20
20
class SocialAuthService
21
21
{
22
+ /**
23
+ * The core socialite library used.
24
+ * @var Socialite
25
+ */
22
26
protected $ socialite ;
23
- protected $ socialAccount ;
24
27
25
- protected $ validSocialDrivers = ['google ' , 'github ' , 'facebook ' , 'slack ' , 'twitter ' , 'azure ' , 'okta ' , 'gitlab ' , 'twitch ' , 'discord ' ];
28
+ /**
29
+ * The default built-in social drivers we support.
30
+ * @var string[]
31
+ */
32
+ protected $ validSocialDrivers = [
33
+ 'google ' ,
34
+ 'github ' ,
35
+ 'facebook ' ,
36
+ 'slack ' ,
37
+ 'twitter ' ,
38
+ 'azure ' ,
39
+ 'okta ' ,
40
+ 'gitlab ' ,
41
+ 'twitch ' ,
42
+ 'discord '
43
+ ];
44
+
45
+ /**
46
+ * Callbacks to run when configuring a social driver
47
+ * for an initial redirect action.
48
+ * Array is keyed by social driver name.
49
+ * Callbacks are passed an instance of the driver.
50
+ * @var array<string, callable>
51
+ */
52
+ protected $ configureForRedirectCallbacks = [];
26
53
27
54
/**
28
55
* SocialAuthService constructor.
@@ -39,7 +66,7 @@ public function __construct(Socialite $socialite)
39
66
public function startLogIn (string $ socialDriver ): RedirectResponse
40
67
{
41
68
$ driver = $ this ->validateDriver ($ socialDriver );
42
- return $ this ->getSocialDriver ($ driver )->redirect ();
69
+ return $ this ->getDriverForRedirect ($ driver )->redirect ();
43
70
}
44
71
45
72
/**
@@ -49,7 +76,7 @@ public function startLogIn(string $socialDriver): RedirectResponse
49
76
public function startRegister (string $ socialDriver ): RedirectResponse
50
77
{
51
78
$ driver = $ this ->validateDriver ($ socialDriver );
52
- return $ this ->getSocialDriver ($ driver )->redirect ();
79
+ return $ this ->getDriverForRedirect ($ driver )->redirect ();
53
80
}
54
81
55
82
/**
@@ -227,7 +254,7 @@ public function detachSocialAccount(string $socialDriver): void
227
254
/**
228
255
* Provide redirect options per service for the Laravel Socialite driver
229
256
*/
230
- public function getSocialDriver (string $ driverName ): Provider
257
+ protected function getDriverForRedirect (string $ driverName ): Provider
231
258
{
232
259
$ driver = $ this ->socialite ->driver ($ driverName );
233
260
@@ -238,6 +265,10 @@ public function getSocialDriver(string $driverName): Provider
238
265
$ driver ->with (['resource ' => 'https://graph.windows.net ' ]);
239
266
}
240
267
268
+ if (isset ($ this ->configureForRedirectCallbacks [$ driverName ])) {
269
+ $ this ->configureForRedirectCallbacks [$ driverName ]($ driver );
270
+ }
271
+
241
272
return $ driver ;
242
273
}
243
274
@@ -248,12 +279,19 @@ public function getSocialDriver(string $driverName): Provider
248
279
* within the `Config/services.php` file.
249
280
* Handler should be a Class@method handler to the SocialiteWasCalled event.
250
281
*/
251
- public function addSocialDriver (string $ driverName , array $ config , string $ socialiteHandler )
252
- {
282
+ public function addSocialDriver (
283
+ string $ driverName ,
284
+ array $ config ,
285
+ string $ socialiteHandler ,
286
+ callable $ configureForRedirect = null
287
+ ) {
253
288
$ this ->validSocialDrivers [] = $ driverName ;
254
289
config ()->set ('services. ' . $ driverName , $ config );
255
290
config ()->set ('services. ' . $ driverName . '.redirect ' , url ('/login/service/ ' . $ driverName . '/callback ' ));
256
291
config ()->set ('services. ' . $ driverName . '.name ' , $ config ['name ' ] ?? $ driverName );
257
292
Event::listen (SocialiteWasCalled::class, $ socialiteHandler );
293
+ if (!is_null ($ configureForRedirect )) {
294
+ $ this ->configureForRedirectCallbacks [$ driverName ] = $ configureForRedirect ;
295
+ }
258
296
}
259
297
}
0 commit comments