laravellaravel-fortify

Why Fortify::authenticateUsing is not triggered?


I have installed manually fortify in my laravel Starter Kit and as I need to add some additive checks on login

I app/Providers/FortifyServiceProvider.php I do :

<?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', [] );
        });

I resources/views/auth/login.blade.phpI defined form :

<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>

in listing of routes I see :

        | 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                                                       |
|   

In config/fortify.php I have :

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

Also in config/app.php I added line

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

What did I miss ?

Modified block :

I found 1 issue and in login blade form I modified form definition with route('login') :

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

but anyway Fortify::authenticateUsing is not triggered.

I do not have any login defintion in file routes/web.php I do not see why in output of

php artisan route:list

command I see lines :

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

Are references above correct ?

Seems I do not have to edit app/Http/Kernel.php manually ?

As originally that was laravel Starter Kit based on Admin templated without fortify can it be references for that Kit? Where can it be checked ?

Thanks!


Solution

  • After analysing your code I have found the issue in naming the form controls.

    So inside the Login form you have changed the email and pasword to 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.

    Since you have changed filed names. It will always give the validation errors. But you have not displayed the validation error. Thats the reason you cannot find the issue.

    Changing from login_email to email and login_password to password will fix the issue.

    <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="{{ __('Введите ваш пароль') }}">
    

    I have sent a PR along with the fix