doutu9810
2018-01-30 12:10
浏览 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.

    打赏 评论

相关推荐 更多相似问题