【问题标题】:Why Fortify::authenticateUsing is not triggered?为什么没有触发 Fortify::authenticateUsing?
【发布时间】:2021-12-06 16:06:26
【问题描述】:

我已经在我的 laravel Starter Kit 中手动安装了 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.php我定义的表格:

<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 个问题,在登录刀片表单中,我使用 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 Starter Kit,没有强化,它可以作为该工具包的引用吗? 在哪里可以查到?

谢谢!

【问题讨论】:

  • 请看修改块
  • 我已经安装 Laravel Telescope 并在登录后检查请求我看到:prnt.sc/2299rqx 我仍然无法理解为什么 FortifyServiceProvider 中的 Fortify::authenticateUsing 没有被触发?
  • 我在github.com/sergeynilov/fortify_error 上传了项目请看一下:我需要通过登录来触发 FortifyServiceProvideris 中的 Fortify::authenticateUsing 并检查是否正确安装了 Fortify。谢谢!

标签: laravel laravel-fortify


【解决方案1】:

分析您的代码后,我发现表单控件的命名存在问题。

所以在登录表单中,您已将表单中的emailpasword 更改为login_email 和密码login_password。因此,当您向应用程序 AuthenticatedSessionController 发送登录请求时,请使用 LoginRequest 验证您的请求。

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

login_email 更改为emaillogin_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 以及修复

【讨论】:

    猜你喜欢
    • 2022-06-10
    • 2021-05-23
    • 2021-06-21
    • 2019-02-19
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    相关资源
    最近更新 更多