Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] Feature: Create account on login #58

Merged
merged 5 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions config/socialstream.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use JoelButcher\Socialstream\Features;

return [

/*
Expand Down Expand Up @@ -60,4 +62,19 @@
'providers' => [
// '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(),
],
];
38 changes: 38 additions & 0 deletions src/Features.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace JoelButcher\Socialstream;

class Features
{
/**
* Determine if the given feature is enabled.
*
* @param string $feature
* @return bool
*/
public static function enabled(string $feature)
{
return in_array($feature, config('socialstream.features', []));
}

/**
* Determine if the application supports creating accounts
* when logging in for the first time via a provider.
*
* @return bool
*/
public static function createsAccountsOnFirstLogin()
{
return static::enabled(static::createAccountOnFirstLogin());
}

/**
* Enable the create account on first login feature.
*
* @return string
*/
public static function createAccountOnFirstLogin()
{
return 'create-account-on-first-login';
}
}
23 changes: 19 additions & 4 deletions src/Http/Controllers/OAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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])
Expand All @@ -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])
);
Expand All @@ -148,12 +149,26 @@ 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) {
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([
Expand Down
2 changes: 1 addition & 1 deletion src/Socialstream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down