Skip to content

Commit

Permalink
Fix RedirectIfTwoFactorAuthenticatable::validateCredentials and regis…
Browse files Browse the repository at this point in the history
…tration login / error
  • Loading branch information
joelbutcher committed Jul 24, 2024
1 parent dad327a commit 7528f50
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
31 changes: 17 additions & 14 deletions src/Actions/AuthenticateOAuthCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ class AuthenticateOAuthCallback implements AuthenticatesOAuthCallback
* Create a new controller instance.
*/
public function __construct(
protected StatefulGuard $guard,
protected CreatesUserFromProvider $createsUser,
protected StatefulGuard $guard,
protected CreatesUserFromProvider $createsUser,
protected CreatesConnectedAccounts $createsConnectedAccounts,
protected UpdatesConnectedAccounts $updatesConnectedAccounts
)
{
) {
//
}

Expand Down Expand Up @@ -85,7 +84,7 @@ public function authenticate(string $provider, ProviderUser $providerAccount): S

// If a user exists, check the features to make sure we can link unlinked existing users.
if ($user) {
if (!Features::authenticatesExistingUnlinkedUsers()) {
if (! Features::authenticatesExistingUnlinkedUsers()) {
// If we cannot link, return an error asking the user to log in to link their account.
return $this->oauthFailed(
error: __('An account already exists with the same email address. Please log in to connect your :provider account.', ['provider' => Providers::name($provider)]),
Expand All @@ -108,17 +107,21 @@ public function authenticate(string $provider, ProviderUser $providerAccount): S
}

// If a user does not exist for the provider account, check if registration is supported.
if (! $this->canRegister()) {
// If registration is not supported, return an error.
return $this->oauthFailed(
error: __('Registration is disabled.'),
provider: $provider,
providerAccount: $providerAccount,
);
if ($this->canRegister()) {
// If registration is supported, register the user.
return $this->register($provider, $providerAccount);
}

// Otherwise, register the user.
return $this->register($provider, $providerAccount);
// Otherwise, return an error.
$error = Route::has('login') && Session::get('socialstream.previous_url') === route('login')
? __('Account not found, please register to create an account.')
: __('Registration is disabled.');

return $this->oauthFailed(
error: $error,
provider: $provider,
providerAccount: $providerAccount,
);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/Actions/RedirectIfTwoFactorAuthenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ protected function validateCredentials($request)
$socialUser = app(ResolvesSocialiteUsers::class)
->resolve($request->route('provider'));

return tap(Socialstream::$userModel::where('email', $socialUser->getEmail())->first(), function ($user) use ($request, $socialUser) {
if (! $user || ! Socialstream::$connectedAccountModel::where('email', $socialUser->getEmail())->first()) {
$this->fireFailedEvent($request, $user);
$connectedAccount = tap(Socialstream::$connectedAccountModel::where('email', $socialUser->getEmail())->first(), function ($connectedAccount) use ($request, $socialUser) {
if (! $connectedAccount) {
$this->fireFailedEvent($request, $connectedAccount->user);

$this->throwFailedAuthenticationException($request);
}
});

return $connectedAccount->user;
}
}
2 changes: 1 addition & 1 deletion src/Http/Responses/OAuthFailedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Session;
use JoelButcher\Socialstream\Concerns\InteractsWithComposer;
use JoelButcher\Socialstream\Contracts\OAuthLoginFailedResponse as OAuthFailedResponseContract;
use JoelButcher\Socialstream\Contracts\OAuthFailedResponse as OAuthFailedResponseContract;
use JoelButcher\Socialstream\Socialstream;

class OAuthFailedResponse implements OAuthFailedResponseContract
Expand Down

0 comments on commit 7528f50

Please sign in to comment.