ThcDelux3
ThcDelux3

Reputation: 11

Adding hCaptcha to Laravel Jetstream (Inertia.js)

I'm using Laravel 8 - Jetstream 2.0 with the Inertia stack.

I installed the Vue hCaptcha component https://github.com/hCaptcha/vue-hcaptcha The Vue component is already in my login form and looks good.

Vue component is working

then I followed this guide https://serversideup.net/laravel-hcaptcha-custom-validation-rule/ and set up the rule for the hCaptcha in laravel.

Now my question where in laravel/jetstream can I set the captcha rule to be required when the form is submitted. So the captcha is used and not only shown.

I know this is a very basic question but I'm pretty new to laravel and trying to get into vue, inertia.js and jetstream.

Upvotes: 0

Views: 1681

Answers (1)

ThcDelux3
ThcDelux3

Reputation: 11

Ok so there is no default Logincontroller in fortitfy so I made my own to validate the captcha in the form. This code is lacking an user friendly error message management but the captcha is working.

Logincontroller

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Rules\ValidHCaptcha;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;


class LoginController extends Controller
{
    public function authenticate(Request $request)
    {

        // Retrive Input
        $validation = $request->only('email', 'password', 'hcaptcharesponse');

        Validator::make($validation, [
            'email' => ['required', 'string', 'email', 'max:255'],
            'password' => ['required'],
            'hcaptcharesponse' => ['required', new ValidHCaptcha()],
        ])->validate();


        try {

            $user = User::where('email', $request->email)->first();

            if ($user && Hash::check($request->password, $user->password)) {

                Auth::login($user);
                return redirect('/dashboard');

            }

        } catch (Exception $e) {
            dd($e->getMessage());

        }
    }
}

Route for Logincontroller

//Custom login controller for Captcha use Route::post('login', 
[LoginController::class, 'authenticate'])->name('login');

Upvotes: 1

Related Questions