Skip to content

Instantly share code, notes, and snippets.

@anubra266
Created January 10, 2021 23:33
Show Gist options
  • Save anubra266/60ad89651c3cfd0d25641def37e64746 to your computer and use it in GitHub Desktop.
Save anubra266/60ad89651c3cfd0d25641def37e64746 to your computer and use it in GitHub Desktop.
Custom Fortify Login

Create Custom Login Class in Laravel Fortify

Open you FortifyServiceProvider.php at App/Providers.

Update your boot method as such

public function boot()
{
        ...
        CustomLogin::handle();
}
    

Copy the contents of CustomLogin.php in this gist to App\Actions\Auth\CustomLogin And in the same directory create CustomAttemptToAuthenticate.php with the contents of this gist.

Modify the handle method of CustomAttemptToAuthenticate.php as you deem fit. Happy Hacking! 𝨾

<?php
namespace App\Actions\Auth;
use Laravel\Fortify\Actions\AttemptToAuthenticate;
use Laravel\Fortify\Fortify;
class CustomAttemptToAuthenticate extends AttemptToAuthenticate
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @param callable $next
* @return mixed
*/
public function handle($request, $next)
{
if (Fortify::$authenticateUsingCallback) {
return $this->handleUsingCustomCallback($request, $next);
}
if ($this->guard->attempt(
$request->only(Fortify::username(), 'password'),
$request->remember
)) {
return $next($request);
}
$this->throwFailedAuthenticationException($request);
}
}
<?php
namespace App\Actions\Auth;
use Laravel\Fortify\Actions\EnsureLoginIsNotThrottled;
use Laravel\Fortify\Actions\PrepareAuthenticatedSession;
use Laravel\Fortify\Actions\RedirectIfTwoFactorAuthenticatable;
use Laravel\Fortify\Fortify;
use Illuminate\Http\Request;
class CustomLogin
{
public static function handle()
{
Fortify::authenticateThrough(function (Request $request) {
return array_filter([
config('fortify.limiters.login') ? null : EnsureLoginIsNotThrottled::class,
RedirectIfTwoFactorAuthenticatable::class,
CustomAttemptToAuthenticate::class,
PrepareAuthenticatedSession::class,
]);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment