-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: authentication when using a custom pipeline for authentication i…
…n Fortify
- Loading branch information
1 parent
d1c5cde
commit 4d16c3d
Showing
4 changed files
with
104 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JoelButcher\Socialstream\Actions; | ||
|
||
use Illuminate\Auth\Events\Failed; | ||
use Illuminate\Contracts\Auth\StatefulGuard; | ||
use JoelButcher\Socialstream\Contracts\ResolvesSocialiteUsers; | ||
use JoelButcher\Socialstream\Socialstream; | ||
use Laravel\Fortify\Actions\AttemptToAuthenticate as BaseAction; | ||
use Laravel\Fortify\Fortify; | ||
use Laravel\Fortify\LoginRateLimiter; | ||
|
||
class AttemptToAuthenticate extends BaseAction | ||
{ | ||
public function __construct( | ||
StatefulGuard $guard, | ||
LoginRateLimiter $limiter, | ||
protected ResolvesSocialiteUsers $resolver | ||
) { | ||
parent::__construct($guard, $limiter); | ||
} | ||
|
||
public function handle($request, $next, $authIdentifier = null) | ||
{ | ||
if (Fortify::$authenticateUsingCallback) { | ||
return $this->handleUsingCustomCallback($request, $next); | ||
} | ||
|
||
// Fallback to Laravel Fortify | ||
if (! $request->route('provider') && $request->route(Fortify::username())) { | ||
return parent::handle($request, $next); | ||
} | ||
|
||
if ($authIdentifier) { | ||
$this->guard->loginUsingId($authIdentifier, Socialstream::hasRememberSessionFeatures()); | ||
|
||
return $next($request); | ||
} | ||
|
||
$socialUser = $this->resolver->resolve($request->route('provider')); | ||
|
||
$connectedAccount = tap(Socialstream::$connectedAccountModel::where('email', $socialUser->getEmail())->first(), function ($connectedAccount) use ($request, $socialUser) { | ||
if (! $connectedAccount) { | ||
event(new Failed($this->guard?->name ?? config('fortify.guard'), user: null, credentials: [ | ||
'provider' => $request->route('provider'), | ||
])); | ||
|
||
$this->throwFailedAuthenticationException($request); | ||
} | ||
}); | ||
|
||
$this->guard->login($connectedAccount->user, Socialstream::hasRememberSessionFeatures()); | ||
|
||
return $next($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters