使用ajax

时间:2015-12-24 12:44:47

标签: laravel login laravel-5.1 throttling

我正在使用ajax的登录表单,我无法设置登录限制。 ThrottlesLogins trait重定向到某处,但我不需要。当用户密码 n 次时,如何返回秒数? 控制器:

<?php

namespace App\Http\Controllers\Login;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Auth;

class LoginController extends Controller
{
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    public function index()
    {
        if (Auth::check()) return redirect(url('/dashboard'));
        else return view('admin.login');
    }

    public function login(Request $request) {
        $this->validate($request, [
            'username' => 'required', 'password' => 'required',
        ]);

        $credentials = $request->only('username', 'password');

        $throttles = $this->isUsingThrottlesLoginsTrait();

        if ($throttles && $this->hasTooManyLoginAttempts($request)) {
            return $this->sendLockoutResponse($request);
        }

        if (Auth::attempt($credentials, $request->has('remember')))
        {
            return $this->handleUserWasAuthenticated($request, $throttles);
            //return response()->json(['code' => 1, 'redirect' => url('/dashboard')]);
        }
        //else return response()->json(['code' => 0]);

        if ($throttles) {
            $this->incrementLoginAttempts($request);
        }
    }
}

routes.php文件

/* Login */
Route::group(array('prefix' => 'login', 'namespace' => 'Login', 'middleware' => 'guest'), function() {
    Route::get('/', 'LoginController@index');
    Route::post('/', 'LoginController@login');
});

1 个答案:

答案 0 :(得分:4)

您可以更改:

if ($throttles && $this->hasTooManyLoginAttempts($request)) {
    return $this->sendLockoutResponse($request);
}

进入

if ($throttles && $this->hasTooManyLoginAttempts($request)) {
    if ($request->ajax()) {
        return response()->json(['lockout_time' => $this->lockoutTime()]);
    }
    else {
       return $this->sendLockoutResponse($request);
    }
}