Laravel/uiでログインバリデーションを実装する方法

Laravel/uiでログインバリデーションを実装する方法

314 回閲覧されました

みなさんこんにちは、jonioです。

昔作ったアプリを公開しようと思い色々と実装しているのですがログイン時にrecaptchaのバリデーションを入れようとしました。

ログイン機能に「Laravel/ui」を使ったのですがLaravelのバージョン8では非推奨になっています。

いまさらBreezeを使ってログイン機能をすり替えようと思わなかったのでそのままやったのですがログインのバリデーションをどのファイルで行えばいいか分からなくてひたすら探してやっと見つけました。

どれだけの需要があるのか分からないですがLaravel/uiでログイン機能を実装してバリデーションのやり方がわからなくて詰まっている人の為にこの記事を残します。

Laravelのバージョン

バージョンは8です。

AuthenticatesUsers.php

「Laravelのプロジェクト > vendor > laravel > ui > auth-backend > AuthenticatesUsers.php」を修正します。

<?php

namespace Illuminate\Foundation\Auth;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

trait AuthenticatesUsers
{
    use RedirectsUsers, ThrottlesLogins;

    /**
     * Show the application's login form.
     *
     * @return \Illuminate\View\View
     */
    public function showLoginForm()
    {
        return view('auth.login');
    }

    /**
     * Handle a login request to the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    public function login(Request $request)
    {
        $this->validateLogin($request);

        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        if (method_exists($this, 'hasTooManyLoginAttempts') &&
            $this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        if ($this->attemptLogin($request)) {
            if ($request->hasSession()) {
                $request->session()->put('auth.password_confirmed_at', time());
            }

            return $this->sendLoginResponse($request);
        }

        // If the login attempt was unsuccessful we will increment the number of attempts
        // to login and redirect the user back to the login form. Of course, when this
        // user surpasses their maximum number of attempts they will get locked out.
        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }

    /**
     * Validate the user login request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
     
     
    //ここから
    protected function validateLogin(Request $request)
    {
        $request->validate([
            $this->username() => 'required|string',
            'password' => 'required|string',
            'g-recaptcha-response' => ['required', 'captcha'],        //この行を追加
        ]);
    }
    //ここまで

バリデーションは73行目〜80行目の間に追加します。

今回はrecaptchaをバリデーションにしているので78行目を追加しています。

これで説明は終わりですが一応recaptchaを実装したい人の為に記事を掲載しておきます。