即使密码错误,Laravel也会登录身份验证

问题描述:

这有点令人困惑,因为我是laravel的新手。它始终将我重定向到applicant/login从应该被重定向到applicant/home的窗体操作的URL使用正确的身份验证。即使密码错误,Laravel也会登录身份验证

这是我在我的控制器代码:

public function authenticate(Request $request) 
{ 
    $credentials = $request->only('email', 'password'); 

    if ($this->guard()->attempt($credentials)) { 
     /* 
     $user = Admin::where('email', $credentials['email'])->first(); 
     // Authentication passed... 
     $this->guard()->loginUsingId($user->id); 
     */ 
     return redirect()->intended('applicant/home'); 
    } 

    return view('applicants.home'); 
} 

下面是我的表单代码:

<form method="post" action="{{ url('/applicants/login') }}"> 
    {!! csrf_field() !!} 

    <div class="form-group has-feedback {{ $errors->has('email') ? ' has-error' : '' }}"> 
     <input type="email" class="form-control" name="email" value="{{ old('email') }}" placeholder="Email"> 
     <span class="glyphicon glyphicon-envelope form-control-feedback"></span> 
     @if ($errors->has('email')) 
      <span class="help-block"> 
      <strong>{{ $errors->first('email') }}</strong> 
     </span> 
     @endif 
    </div> 

    <div class="form-group has-feedback{{ $errors->has('password') ? ' has-error' : '' }}"> 
     <input type="password" class="form-control" placeholder="Password" name="password"> 
     <span class="glyphicon glyphicon-lock form-control-feedback"></span> 
     @if ($errors->has('password')) 
      <span class="help-block"> 
      <strong>{{ $errors->first('password') }}</strong> 
     </span> 
     @endif 

    </div> 
    <div class="row"> 
     <div class="col-xs-8"> 
      <div class="checkbox icheck"> 
       <label> 
        <!--<input type="checkbox" name="remember"> Remember Me --> 
       </label> 
      </div> 
     </div> 
     <!-- /.col --> 
     <div class="col-xs-4"> 
      <button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button> 
     </div> 
     <!-- /.col --> 
    </div> 
</form> 
+0

你用过laravel默认验证已登录登录或授权访问一个页面? – Gunaseelan

+0

您是否在需要登录的页面上使用auth定义了路由器中间件? – Jorn

Laravel都默认身份验证方法。

品牌:php artisan make:auth与文件

文件夹将被创建。

/resources/views/auth/login.blade.php

你有你的登录表单。

现在你的表格必须看起来像在这里:

<form method="post" action="{{ url('/login') }}"> 
      {!! csrf_field() !!} 

      <div class="form-group has-feedback {{ $errors->has('email') ? ' has-error' : '' }}"> 
       <input type="email" class="form-control" name="email" value="{{ old('email') }}" placeholder="Email"> 
       <span class="glyphicon glyphicon-envelope form-control-feedback"></span> 
       @if ($errors->has('email')) 
        <span class="help-block"> 
        <strong>{{ $errors->first('email') }}</strong> 
       </span> 
       @endif 
      </div> 

      <div class="form-group has-feedback{{ $errors->has('password') ? ' has-error' : '' }}"> 
       <input type="password" class="form-control" placeholder="Password" name="password"> 
       <span class="glyphicon glyphicon-lock form-control-feedback"></span> 
       @if ($errors->has('password')) 
        <span class="help-block"> 
        <strong>{{ $errors->first('password') }}</strong> 
       </span> 
       @endif 

      </div> 
      <div class="row"> 
       <div class="col-xs-8"> 
        <div class="checkbox icheck"> 
         <label> 
          <!--<input type="checkbox" name="remember"> Remember Me --> 
         </label> 
        </div> 
       </div> 
       <!-- /.col --> 
       <div class="col-xs-4"> 
        <button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button> 
       </div> 
       <!-- /.col --> 
      </div> 
     </form> 

而且在

应用程序/ HTTP /控制器/ AUTH

你有

的LoginController .php

,里面的:

class LoginController extends Controller 
{ 
you must have this: 

protected $redirectTo = '/home'; home, or what you want;) 
} 

另外,请检查web.php(routes文件),你必须:

Auth::routes(); 
+0

感谢您的帮助,我已经明白了!非常感谢! :) –

+0

@JayzdeVera欢迎队友。如果我的回答对您有帮助,请将其标记为正确;)如果您有任何关于Laravel的问题,可以将该网址置于此答案的评论中,我会尽力为您提供帮助。快乐代码! –

我认为你可以尝试一下本作,你可能会帮助:

只需使用laravel默认身份验证登录和注册用户,如:

php artisan make:auth 

希望对你有帮助!

+0

感谢您的帮助!我已经弄清楚了! –

+0

@JayzdeVera:很高兴帮助,如果我的答案是你的解决方案,那么请接受我的回答 –

正如@LluísPuig Ferrer所建议的,使用Laravel构建的Auth。网上有很多文档。

确保在web.php内定义路线时使用auth middleware。当用户想要访问使用该auth middleware他们要么被询问是否在

Route::group(['middleware' => 'auth'], function() { 
    // All the routes that require login authentication 
}; 
+0

我知道了! :)谢谢btw :) –