doutu9810 2018-01-30 12:10 采纳率: 0%
浏览 56
已采纳

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

I used Auth middleware in Laravel 5.4 for my login page. Now I've added a captcha package from https://packagist.org/packages/bonecms/laravel-captcha to add captcha to the login page but it does not validate captcha correctly. What is the problem?How should I change my controller?

this is my view:

<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>

this is my controller:

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条回答 默认 最新

  • dongqiongjiong4740 2018-01-30 13:06
    关注

    Route login goes to LoginController::login() method. Add your validation rules to LoginController::login() method.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。
  • ¥15 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串