Skip to content

Commit

Permalink
Add events to allow extending/overriding needed extension behavior
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed Apr 1, 2022
1 parent a04c190 commit 7de5ad5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 15 deletions.
17 changes: 13 additions & 4 deletions classes/BaseProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ abstract class BaseProvider

protected $settings;

protected static $configCallbacks = [];

public function __construct($driver = null)
{
$this->driver = $driver;
Expand Down Expand Up @@ -48,9 +50,11 @@ protected function initialize()
*/
protected function buildProvider($config, $app)
{
return Socialite::buildProvider(
$this->provider, $config
);
foreach (self::$configCallbacks as $callback) {
$config = $callback($config, $this);
}

return Socialite::buildProvider($this->provider, $config);
}

public function getDriver()
Expand Down Expand Up @@ -90,7 +94,7 @@ public function isEnabled()
return !empty($this->getSetting('status', 0));
}

public function shouldConfirmEmail()
public function shouldConfirmEmail($providerUser)
{
return FALSE;
}
Expand Down Expand Up @@ -150,4 +154,9 @@ abstract public function redirectToProvider();
* @return \Laravel\Socialite\AbstractUser
*/
abstract public function handleProviderCallback();

public static function extendConfig(callable $callback)
{
self::$configCallbacks[] = $callback;
}
}
43 changes: 32 additions & 11 deletions classes/ProviderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class ProviderManager
*/
protected $extensionManager;

protected $resolveUserTypeCallbacks = [];

protected function initialize()
{
$this->extensionManager = ExtensionManager::instance();
Expand Down Expand Up @@ -166,6 +168,11 @@ public function makeProvider($className, $providerInfo = null)
return new $className($code);
}

public function resolveUserType(callable $callback)
{
$this->resolveUserTypeCallbacks[] = $callback;
}

/**
* Executes an entry point for registered social providers, defined in routes.php file.
*
Expand All @@ -191,14 +198,23 @@ public static function runEntryPoint($code, $action)
if ($action === 'auth') {
session()->put('igniter_socialite_redirect', [$successUrl, $errorUrl]);

event('igniter.socialite.beforeRedirect', [$providerClass]);

return $providerClass->redirectToProvider();
}

if ($redirect = $manager->handleProviderCallback($providerClass, $errorUrl))
return $redirect;

// Grab the user associated with this provider. Creates or attach one if need be.
return $manager->completeCallback();
$redirectUrl = $manager->completeCallback();

session()->forget([
'igniter_socialite_redirect',
'igniter_socialite_provider',
]);

return $redirectUrl ?: redirect()->to($successUrl);
}
catch (Exception $ex) {
flash()->error($ex->getMessage());
Expand All @@ -210,7 +226,6 @@ public static function runEntryPoint($code, $action)
public function completeCallback()
{
$sessionProvider = session()->get('igniter_socialite_provider');
[$successUrl, $errorUrl] = session()->get('igniter_socialite_redirect', ['/', '/login']);

if (!$sessionProvider || !isset($sessionProvider['user']))
return;
Expand All @@ -219,6 +234,9 @@ public function completeCallback()
if (is_null($provider = Provider::find($sessionProvider['id'])))
return;

if (event('igniter.socialite.completeCallback', [$providerUser, $provider], TRUE) === TRUE)
return;

$user = $this->createOrUpdateUser($providerUser, $provider);

$provider->applyUser($user)->save();
Expand All @@ -231,13 +249,6 @@ public function completeCallback()
Auth::login($user, TRUE);

Event::fire('igniter.socialite.login', [$user], TRUE);

session()->forget([
'igniter_socialite_redirect',
'igniter_socialite_provider_id',
]);

return redirect()->to($successUrl);
}

protected function handleProviderCallback($providerClass, $errorUrl)
Expand All @@ -246,7 +257,7 @@ protected function handleProviderCallback($providerClass, $errorUrl)
$providerUser = $providerClass->handleProviderCallback();

$provider = Provider::firstOrNew([
'user_type' => 'customers',
'user_type' => $this->resolveUserTypeCallback(),
'provider' => $providerClass->getDriver(),
'provider_id' => $providerUser->id,
]);
Expand All @@ -259,7 +270,7 @@ protected function handleProviderCallback($providerClass, $errorUrl)
'user' => $providerUser,
]);

if (!strlen($providerUser->email) || $providerClass->shouldConfirmEmail())
if ($providerClass->shouldConfirmEmail())
return redirect()->to(page_url('/confirm-email'));
}
catch (Exception $ex) {
Expand Down Expand Up @@ -289,4 +300,14 @@ protected function createOrUpdateUser(ProviderUser $providerUser, Provider $prov

return $user;
}

protected function resolveUserTypeCallback()
{
foreach ($this->resolveUserTypeCallbacks as $callback) {
if ($userType = $callback($this))
return $userType;
}

return 'customers';
}
}
5 changes: 5 additions & 0 deletions socialiteproviders/Facebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public function handleProviderCallback()
{
return Socialite::driver($this->driver)->user();
}

public function shouldConfirmEmail($providerUser)
{
return !strlen($providerUser->email);
}
}
5 changes: 5 additions & 0 deletions socialiteproviders/Google.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ public function handleProviderCallback()
{
return Socialite::driver($this->driver)->user();
}

public function shouldConfirmEmail($providerUser)
{
return !strlen($providerUser->email);
}
}
5 changes: 5 additions & 0 deletions socialiteproviders/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ public function handleProviderCallback()
{
return Socialite::driver($this->driver)->user();
}

public function shouldConfirmEmail($providerUser)
{
return !strlen($providerUser->email);
}
}

0 comments on commit 7de5ad5

Please sign in to comment.