如何在Laravel 5.4中使用验证码在auth中间件中

时间:2018-01-30 12:10:19

标签: php laravel-5.4 captcha laravel-middleware

我在Auth middleware中使用Laravel 5.4作为我的登录页面。现在我已经从https://packagist.org/packages/bonecms/laravel-captcha添加了一个captcha包来将验证码添加到登录页面,但它没有正确验证验证码。问题是什么?我应该如何更换控制器?

这是我的观点:

<form class="form-horizontal" method="POST" action="{{ route('login') }}" style="margin:0 auto;padding: 0 !important;">
                    {{ csrf_field() }}

                    <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                        <label for="email" class="col-md-4 control-label" id="emailadd">ایمیل</label>

                        <div class="col-md-8">
                            <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>

                            @if ($errors->has('email'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('email') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                        <label for="password" class="col-md-4 control-label" id="passwordbox">رمز عبور</label>

                        <div class="col-md-8">
                            <input id="password" type="password" class="form-control" name="password" required>

                            @if ($errors->has('password'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('password') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group{{ $errors->has('captcha') ? ' has-error' : '' }}">

                        <label for="captcha" class="col-md-4 control-label" id="emailadd">کد امنیتی</label>

                        <div class="col-md-8">
                            <div style="display:block;margin:10px auto;">@captcha</div>
                            <input type="text" id="captcha" name="captcha" class="form-control" required>

                            @if ($errors->has('captcha'))

                                <span class="help-block">

                                    <strong>{{ $errors->first('captcha') }}</strong>

                                </span>

                            @endif

                        </div>

                    </div>


                    <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                        <div class="col-md-12">
                        <button type="submit" class="btn" style="width:100%;background-color:#00aeef;color:#ffffff;margin: 10px 0px">
                            login
                        </button>
                        </div>

                    </div>
                </form>

这是我的控制者:

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Lang;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    protected function sendFailedLoginResponse(Request $request)
    {
        $this->validate($request, [
            'captcha' => 'required|captcha'
        ],
        [   'captcha.required' => 'کد امنیتی را وارد نکرده اید.',
            'captcha.captcha' => 'کد امنیتی اشتباه است',
        ]);

        if ( ! User::where('email', $request->email)->first() ) {
            return redirect()->back()
                ->withInput($request->only($this->username(), 'remember'))
                ->withErrors([
                    $this->username() => Lang::get('آدرس ایمیل اشتباه است'),
                ]);
        }

        if ( ! User::where('email', $request->email)->where('password', bcrypt($request->password))->first() ) {
            return redirect()->back()
                ->withInput($request->only($this->username(), 'remember'))
                ->withErrors([
                    'password' => Lang::get('رمز عبور اشتباه است'),
                ]);
        }

    }

}

1 个答案:

答案 0 :(得分:1)

路由登录转到LoginController :: login()方法。将验证规则添加到LoginController :: login()方法。