mewebstudio / captcha

Captcha for Laravel 5/6/7/8/9/10/11
MIT License
2.43k stars 450 forks source link

Captcha not working with Fortify Laravel 8 login (Fortify :: authenticateUsing) #226

Open Slabar opened 2 years ago

Slabar commented 2 years ago

Captcha does not pass validation. What could be the problem?

FortifyServiceProvider.php

Fortify::authenticateUsing(function (Request $request) {

            Validator::make($request->all(), [
                'captcha' => 'required|captcha'
            ])->validate();

            $user = User::where('email', $request->email)->first();
            if ($user &&
                Hash::check($request->password, $user->password)) {
                return $user;
            }
});

LoginForm

<form method="POST" action="{{route("login")}}">
                @csrf
                <div class="form-group">
                    <label for="email">{{__('Login')}}</label>
                    <input id="email" type="text" class="form-control" name="email" tabindex="1" required autofocus>
                    @error('email')
                    {{ $message }}
                    @enderror
                </div>

                <div class="form-group">
                    <div class="d-block">
                        <label for="password" class="control-label">{{__('Password')}}</label>
                        <div class="float-right">
                            <a href="auth-forgot-password.html" class="text-small">
                                Forgot Password?
                            </a>
                        </div>
                    </div>
                    <input id="password" type="password" class="form-control" name="password" tabindex="2" required>
                    @error('password')
                    {{ $message }}
                    @enderror
                </div>

                <div class="form-group">
                    <div class="custom-control custom-checkbox">
                        <input type="checkbox" name="remember" class="custom-control-input" tabindex="3" id="remember-me">
                        <label class="custom-control-label" for="remember-me">Remember Me</label>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-6 captcha">
                        <label for="password" class="d-block">{{__('Captcha Image')}}</label>
                        <span>{!! captcha_img() !!}</span>
                        <button type="button" class="btn btn-danger" class="reload" id="reload">
                            ↻
                        </button>
                    </div>
                    <div class="form-group col-6">
                        <label for="password" class="d-block">{{__('Captcha Text')}}</label>
                        <input id="captcha" type="text" class="form-control @error('captcha') is-invalid @enderror" name="captcha" placeholder="{{__('Enter Captcha')}}">
                        @error('captcha')
                        {{ $message }}
                        @enderror
                    </div>
                </div>

                <div class="form-group">
                    <button type="submit" class="btn btn-primary btn-lg btn-block" tabindex="4">
                        Login
                    </button>
                </div>
</form>
isnunasrudin commented 2 years ago

This is the same with my problem. That's because the "authenticateUsing" method is called 2 times

isnunasrudin commented 2 years ago

The solving :

Fortify::authenticateUsing(function (Request $request) {

            if(Validator::make($request->all(), [
                'captcha' => 'required|captcha'
            ])) return;

            $user = User::where('email', $request->email)->first();
            if ($user &&
                Hash::check($request->password, $user->password)) {
                return $user;
            }

});
kingroho commented 1 year ago

Hello @isnunasrudin authenticateUsing() indeed runs twice. Even when I put a blank callback, it still uses its default login mechanism.