0

在 Laravel 5.6 中,我使用它的默认身份验证功能。但是我已经实现了一个模式来替换默认的登录表单。我正在尝试保持登录模式表单打开并在登录尝试失败时显示错误。我的登录模式表单如下所示:

<!-- Modal HTML Markup -->
<div id="login-modal" class="modal fade">
 <div class="modal-dialog" role="document">
  <div class="modal-content">
   <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    <h1 class="modal-title">Login</h1>
   </div>

    <div class="modal-body">
     <div class="cs-login-form">
      <form role="form" method="POST" action="{{ url('/login') }}">
       <div class="input-holder">
        {{ csrf_field() }}
        <div class="form-group">
         <label for="email" class="control-label">E-Mail Address</label>
          <div>
           <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" autofocus>
          </div>
         </div>
        </div>
        <div class="input-holder">
         <div class="form-group">
          <label for="password" class="control-label">Password</label>
         <div>
         <input id="password" type="password" class="form-control" name="password">
         </div>
        </div>

       </div>


       <div class="form-group">
       <div>
        <div class="checkbox">
         <label>
          <input type="checkbox" name="remember"> Remember Me
         </label>
        </div>
       </div>
       </div>

       <div class="form-group">
        <div>
         <button type="submit" class="btn btn-primary">
          Login
         </button>
         <a class="btn btn-link" href="{{ url('/password/reset') }}">
          Forgot Your Password?
         </a>
        </div>
       </div>
      </form>
     </div>    
    </div>
   </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
 </div>

当前,当用户输入无效凭据时,模式会消失,并且用户会被重定向回主页而没有错误。我的登录控制器没有改变,看起来像这样

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

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 = '/home';

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

3 回答 3

1

您可以使用 ajax 调用提交您的凭据,然后在您的成功方法中,您可以检查它是否成功,如果不正常则显示错误。如果没问题,您可以手动隐藏模式并将用户重定向到主页。

于 2018-06-03T08:12:18.987 回答
0

正如Laravel 的文档所解释的那样,您需要手动设置一个函数。您还需要一个路由和一个 ajax 请求来进行登录。

自定义登录路径:

Route::post('/login', 'LoginController@authenticate');

在您看来,您首先需要使用 csrf 令牌设置元标记以保护自己免受攻击:

<meta name="csrf-token" content="{{ csrf_token() }}">

然后在你的ajax中你可以这样做:

$.ajax({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    type: 'POST',
    url: '/login',
    data: [email: '', password: ''],
    success: function(controllerResponse) {
        if (!controllerResponse) {
           // here show a hidden field inside your modal by setting his visibility 
        } else {
           // controller return was true so you can redirect or do whatever you wish to do here
        }
    }
});

自定义控制器方法可能是这样的:

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

    if (Auth::attempt($credentials)) {
        // Authentication passed...
        return true;
    } else {
        return false;
    }
}

*重要提示:请注意,我没有完全编码evrything,因为stackoverflow是关于解决问题而不是为其他人编码,所以你需要弄清楚如何用js选择表单数据,并创建错误消息并在控制器时显示它返回假。

此外,控制器非常基础,您可以通过控制某个用户的最大登录尝试等功能对其进行改进。*

于 2018-06-03T08:47:15.350 回答
-1

您可以使用会话变量 $_SESSION['error']。并在您的登录表单上写一个 if 条件,例如

if(isset($_SESSION['error']) echo "你的错误信息。")

然后就取消设置会话变量

于 2018-06-03T09:23:34.780 回答