为什么Fortify::authenticateUsing没有被触发?

Why Fortify::authenticateUsing is not triggered?

我已在我的 laravel 初学者工具包中手动安装 fortify,因为我需要在登录时添加一些附加检查

我app/Providers/FortifyServiceProvider.php我愿意:

<?php

namespace App\Providers;

use App\Actions\Fortify\CreateNewUser;
use App\Actions\Fortify\ResetUserPassword;
use App\Actions\Fortify\UpdateUserPassword;
use App\Actions\Fortify\UpdateUserProfileInformation;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\ValidationException;
use Laravel\Fortify\Fortify;

class FortifyServiceProvider extends ServiceProvider
{
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {

        Fortify::loginView(function () {  // THIS ACTION IS TRIGGERED
            return view('auth.login', [] );
        });

        Fortify::authenticateUsing(function (Request $request) { // BUT THIS ACTION IS NOT TRIGGERED
            $user = User::where('email', $request->email)->first();
            \Log::info(  varDump($user, ' -1 $user::') );
            \Log::info(  varDump($request, ' -2 $request::') );
            $request = request();
            if ($user && Hash::check($request->password, $user->password)) {
                if ( $user->status === 'A') {
                    return $user;
                }
                else {
                    throw ValidationException::withMessages([
                        Fortify::username() => "Account is inactive",
                    ]);
                }

            }
        });

        Fortify::registerView(function () {
            \Log::info(  ' -33 registerView::');
            return view('auth.register', [] );
        });

我resources/views/auth/login.blade.phpI定义的形式:

<form action="/login" method="POST" enctype="multipart/form-data">
    @csrf
    
    <div class="row">
        
        <div class="row mb-4">
            <div class="col-lg-4">
                <label class="form-label" for="example-email-input">{{ __('email') }}</label>
            </div>
            <div class="col-lg-8">
                <input type="email" class="form-control" id="example-email-input"
                       name="example-email-input" placeholder="">
            </div>
        </div>
        
        <div class="row mb-4">
            <div class="col-lg-4">
                <label class="form-label" for="example-password-input">{{ __('password') }}</label>
            </div>
            <div class="col-lg-8">
                <input type="password" class="form-control" id="example-password-input"
                       name="example-password-input" placeholder="">
            </div>
        </div>
        
        
        <div class="d-flex justify-content-end">
            <div class="mr-auto">&nbsp;</div>
            <div class="m-2" >
                <button type="button" href="{{ route('home') }}" class="btn btn-secondary">{{ __('Home') }}</button>
            </div>
            <div class="m-2">
                <button type="submit" class="btn btn-primary mx-4 ">{{ __('login') }}</button>
            </div>
        </div>
    
    </div>

</form>

在我看到的路线列表中:

        | GET|HEAD      | login                                                      | login                           | Laravel\Fortify\Http\Controllers\AuthenticatedSessionController@create          | web                                                       |
|        |               |                                                            |                                 |                                                                                 | App\Http\Middleware\RedirectIfAuthenticated:web           |
|        | POST          | login                                                      | generated::uIOPlOK71yjjoFFT     | Laravel\Fortify\Http\Controllers\AuthenticatedSessionController@store           | web                                                       |
|   

在 config/fortify.php 我有 :

'features' => [
    Features::registration(),
    Features::resetPasswords(),
     Features::emailVerification(),
    Features::updateProfileInformation(),
    Features::updatePasswords(),
    Features::twoFactorAuthentication([
        'confirmPassword' => true,
    ]),
],

也在 config/app.php 我添加了行

'providers' => [
...
    App\Providers\FortifyServiceProvider::class,

我错过了什么?

修改块:

我发现了 1 个问题,在登录 blade 表单中,我使用 route('login') 修改了表单定义:

            <form action="{{ route('login') }}" method="POST" enctype="multipart/form-data">
                @csrf

但无论如何 Fortify::authenticateUsing 没有被触发。

我在文件 routes/web 中没有任何登录定义。php 我不明白为什么

的输出
php artisan route:list

命令 我看到线条:

|        | POST          | login                                                      | generated::spwtVnYkPvigASwV     | Laravel\Fortify\Http\Controllers\AuthenticatedSessionController@store           | web                                                       |
|        |               |                                                            |                                 |                                                                                 | App\Http\Middleware\RedirectIfAuthenticated:web           |
|        |               |                                                            |                                 |                                                                                 | Illuminate\Routing\Middleware\ThrottleRequests:login      |

以上引用是否正确?

看来我不需要手动编辑 app/Http/Kernel.php?

最初是 laravel 基于管理员模板的初学者工具包,没有 fortify 它可以作为该工具包的参考吗? 哪里可以查到?

谢谢!

分析你的代码后,我发现了命名表单控件的问题。

因此在登录表单中,您已将 emailpasword 更改为 login_email and password login_password inside the form. So when ever you send Login request to application AuthenticatedSessionController validate your request by using LoginRequest

由于您更改了文件名。它总是会给出验证错误。但是您没有显示验证错误。这就是你找不到问题的原因。

login_email 更改为 email 并将 login_password 更改为 password 将解决此问题。

<input type="email" class="form-control" id="login_email" name="email" placeholder="{{ __('Введите ваш е-мейл') }}">

<input type="password" class="form-control" id="login_password"                                       name="login_password" placeholder="{{ __('Введите ваш пароль') }}">

我已将 PR 与修复程序一起发送