如何在 Laravel 5.4 的身份验证中间件中使用验证码



我在登录页面中使用了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 AppHttpControllersAuth;
use AppHttpControllersController;
use AppUser;
use IlluminateFoundationAuthAuthenticatesUsers;
use IlluminateHttpRequest;
use IlluminateSupportFacadesLang;
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('رمز عبور اشتباه است'),
                ]);
        }
    }
}

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

相关内容

  • 没有找到相关文章

最新更新