Laravel8 fortify 登录时自定义操作

暂无标签

一、添加配置

打开config/fortify.php
在文件底部的features数组之后添加代码

'pipelines' => [
    'login'=>[
        config('fortify.limiters.login') ? null : Laravel\Fortify\Actions\EnsureLoginIsNotThrottled::class,
        Features::enabled(Features::twoFactorAuthentication()) ? RedirectIfTwoFactorAuthenticatable::class : null,
        App\Http\Authenticate\AttemptToAuthenticate::class,
        Laravel\Fortify\Actions\PrepareAuthenticatedSession::class,
    ],
],

config如果有缓存要清理下。

二、新建文件

新建文件App\Http\Authenticate\AttemptToAuthenticate.php(没有 Authenticate 目录,自己建)
AttemptToAuthenticate.php 代码如下:

<?php

namespace App\Http\Authenticate;
use Laravel\Fortify\Actions\AttemptToAuthenticate as AttemptToAuthenticates;
use Laravel\Fortify\Fortify;

class AttemptToAuthenticate extends AttemptToAuthenticates
{
        /**
        * 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->filled('remember'))
        ) {
            // 代码写这里
            return $next($request);
        }
        $this->throwFailedAuthenticationException($request);
        }    
}