diff --git a/classes/BaseProvider.php b/classes/BaseProvider.php index 090de92..2d0eb45 100644 --- a/classes/BaseProvider.php +++ b/classes/BaseProvider.php @@ -18,6 +18,8 @@ abstract class BaseProvider protected $settings; + protected static $configCallbacks = []; + public function __construct($driver = null) { $this->driver = $driver; @@ -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() @@ -90,7 +94,7 @@ public function isEnabled() return !empty($this->getSetting('status', 0)); } - public function shouldConfirmEmail() + public function shouldConfirmEmail($providerUser) { return FALSE; } @@ -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; + } } diff --git a/classes/ProviderManager.php b/classes/ProviderManager.php index 82f3196..ff2e6b8 100644 --- a/classes/ProviderManager.php +++ b/classes/ProviderManager.php @@ -38,6 +38,8 @@ class ProviderManager */ protected $extensionManager; + protected $resolveUserTypeCallbacks = []; + protected function initialize() { $this->extensionManager = ExtensionManager::instance(); @@ -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. * @@ -191,6 +198,8 @@ public static function runEntryPoint($code, $action) if ($action === 'auth') { session()->put('igniter_socialite_redirect', [$successUrl, $errorUrl]); + event('igniter.socialite.beforeRedirect', [$providerClass]); + return $providerClass->redirectToProvider(); } @@ -198,7 +207,14 @@ public static function runEntryPoint($code, $action) 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()); @@ -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; @@ -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(); @@ -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) @@ -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, ]); @@ -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) { @@ -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'; + } } diff --git a/socialiteproviders/Facebook.php b/socialiteproviders/Facebook.php index e1cf2a4..37e7ba2 100644 --- a/socialiteproviders/Facebook.php +++ b/socialiteproviders/Facebook.php @@ -48,4 +48,9 @@ public function handleProviderCallback() { return Socialite::driver($this->driver)->user(); } + + public function shouldConfirmEmail($providerUser) + { + return !strlen($providerUser->email); + } } diff --git a/socialiteproviders/Google.php b/socialiteproviders/Google.php index 0ae9d34..d0d0100 100644 --- a/socialiteproviders/Google.php +++ b/socialiteproviders/Google.php @@ -55,4 +55,9 @@ public function handleProviderCallback() { return Socialite::driver($this->driver)->user(); } + + public function shouldConfirmEmail($providerUser) + { + return !strlen($providerUser->email); + } } diff --git a/socialiteproviders/Twitter.php b/socialiteproviders/Twitter.php index 5cefbcf..623bb90 100644 --- a/socialiteproviders/Twitter.php +++ b/socialiteproviders/Twitter.php @@ -57,4 +57,9 @@ public function handleProviderCallback() { return Socialite::driver($this->driver)->user(); } + + public function shouldConfirmEmail($providerUser) + { + return !strlen($providerUser->email); + } }