From d5a182ae29075ff24dde00c4f81e5f4d0b940ea2 Mon Sep 17 00:00:00 2001 From: Joel Butcher Date: Fri, 29 Jan 2021 16:34:44 +0000 Subject: [PATCH 1/5] wip --- src/Features.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/Features.php diff --git a/src/Features.php b/src/Features.php new file mode 100644 index 00000000..60f8cd57 --- /dev/null +++ b/src/Features.php @@ -0,0 +1,38 @@ + Date: Fri, 29 Jan 2021 16:34:48 +0000 Subject: [PATCH 2/5] wip --- config/socialstream.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/config/socialstream.php b/config/socialstream.php index daad5e66..bb7172c4 100644 --- a/config/socialstream.php +++ b/config/socialstream.php @@ -1,5 +1,7 @@ [ // 'github', ], + + /* + |-------------------------------------------------------------------------- + | Features + |-------------------------------------------------------------------------- + | + | Some of Socialstreams's features are optional. You may disable the features + | by removing them from this array. You're free to only remove some of + | these features or you can even remove all of these if you need to. + | + */ + + 'features' => [ + // Features::createAccountOnFirstLogin(), + ] ]; From e456a3ca337bbeb4ada74aafdfe6ece610273ffb Mon Sep 17 00:00:00 2001 From: Joel Butcher Date: Fri, 29 Jan 2021 16:47:32 +0000 Subject: [PATCH 3/5] create account on first login --- src/Http/Controllers/OAuthController.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Http/Controllers/OAuthController.php b/src/Http/Controllers/OAuthController.php index 0114db5f..2f0fe25f 100644 --- a/src/Http/Controllers/OAuthController.php +++ b/src/Http/Controllers/OAuthController.php @@ -10,6 +10,7 @@ use JoelButcher\Socialstream\Contracts\CreatesConnectedAccounts; use JoelButcher\Socialstream\Contracts\CreatesUserFromProvider; use JoelButcher\Socialstream\Contracts\GeneratesProviderRedirect; +use JoelButcher\Socialstream\Features; use JoelButcher\Socialstream\Socialstream; use Laravel\Jetstream\Jetstream; use Laravel\Socialite\Facades\Socialite; @@ -72,7 +73,7 @@ public function __construct( */ public function redirectToProvider(Request $request, string $provider, GeneratesProviderRedirect $generator) { - session()->put('url.previous', back()->getTargetUrl()); + session()->put('socialstream.previous_url', back()->getTargetUrl()); return $generator->generate($provider); } @@ -122,7 +123,7 @@ public function handleProviderCallback(Request $request, string $provider) } // Registration... - if (session()->get('url.previous') === route('register')) { + if (session()->get('socialstream.previous_url') === route('register')) { if ($account) { return redirect()->route('register')->withErrors( __('An account with that :Provider sign in already exists, please login.', ['provider' => $provider]) @@ -135,7 +136,7 @@ public function handleProviderCallback(Request $request, string $provider) ); } - if (Jetstream::newUserModel()->where('email', $providerAccount->getEmail())->first()) { + if (Jetstream::newUserModel()->where('email', $providerAccount->getEmail())->exists()) { return redirect()->route('register')->withErrors( __('An account with that email address already exists. Please login to connect your :Provider account.', ['provider' => $provider]) ); @@ -148,12 +149,16 @@ public function handleProviderCallback(Request $request, string $provider) return redirect(config('fortify.home')); } - if (! $account) { + if (! Features::createsAccountsOnFirstLogin() && !$account) { return redirect()->route('login')->withErrors( __('An account with this :Provider sign in was not found. Please register or try a different sign in method.', ['provider' => $provider]) ); } + if (Features::createsAccountsOnFirstLogin() && !$account) { + $user = $this->createsUser->create($provider, $providerAccount); + } + $this->guard->login($account->user, config('socialstream.remember')); $account->user->forceFill([ From cadc648db2f5d161c652a958fc7ffc2cf3301b39 Mon Sep 17 00:00:00 2001 From: Joel Butcher Date: Fri, 29 Jan 2021 17:01:48 +0000 Subject: [PATCH 4/5] wip --- config/socialstream.php | 2 +- src/Features.php | 4 ++-- src/Socialstream.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/config/socialstream.php b/config/socialstream.php index bb7172c4..243c9b8b 100644 --- a/config/socialstream.php +++ b/config/socialstream.php @@ -76,5 +76,5 @@ 'features' => [ // Features::createAccountOnFirstLogin(), - ] + ], ]; diff --git a/src/Features.php b/src/Features.php index 60f8cd57..9fa2b31f 100644 --- a/src/Features.php +++ b/src/Features.php @@ -18,7 +18,7 @@ public static function enabled(string $feature) /** * Determine if the application supports creating accounts * when logging in for the first time via a provider. - * + * * @return bool */ public static function createsAccountsOnFirstLogin() @@ -35,4 +35,4 @@ public static function createAccountOnFirstLogin() { return 'create-account-on-first-login'; } -} \ No newline at end of file +} diff --git a/src/Socialstream.php b/src/Socialstream.php index 33cfe43a..5fa2144d 100644 --- a/src/Socialstream.php +++ b/src/Socialstream.php @@ -56,7 +56,7 @@ public static function hasSupportFor(string $service) /** * Find a connected account instance fot a given provider and provider ID. - * + * * @param string $provider * @param string $providerId * @return mixed From 547acd67a4afeaa9571914a6ca8aa034c9d7d781 Mon Sep 17 00:00:00 2001 From: Joel Butcher Date: Fri, 29 Jan 2021 17:04:33 +0000 Subject: [PATCH 5/5] wip --- src/Http/Controllers/OAuthController.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Http/Controllers/OAuthController.php b/src/Http/Controllers/OAuthController.php index 2f0fe25f..4ee30f04 100644 --- a/src/Http/Controllers/OAuthController.php +++ b/src/Http/Controllers/OAuthController.php @@ -149,16 +149,26 @@ public function handleProviderCallback(Request $request, string $provider) return redirect(config('fortify.home')); } - if (! Features::createsAccountsOnFirstLogin() && !$account) { + if (! Features::createsAccountsOnFirstLogin() && ! $account) { return redirect()->route('login')->withErrors( __('An account with this :Provider sign in was not found. Please register or try a different sign in method.', ['provider' => $provider]) ); } - if (Features::createsAccountsOnFirstLogin() && !$account) { + if (Features::createsAccountsOnFirstLogin() && ! $account) { + if (Jetstream::newUserModel()->where('email', $providerAccount->getEmail())->exists()) { + return redirect()->route('login')->withErrors( + __('An account with that email address already exists. Please login to connect your :Provider account.', ['provider' => $provider]) + ); + } + $user = $this->createsUser->create($provider, $providerAccount); + + $this->guard->login($user, config('socialstream.remember')); + + return redirect(config('fortify.home')); } - + $this->guard->login($account->user, config('socialstream.remember')); $account->user->forceFill([