设置记住ajax laravel登录

时间:2017-02-28 13:34:19

标签: php ajax laravel

使用laravel 5.4重建我的系统。 我做一个有效的ajax登录,但是我想在auth中添加remember选项。 文档说这关于设置记住:

if (Auth::attempt(['email' => $email, 'password' => $password], $remember)) {
    // The user is being remembered...
}

但是由于我的logincontroller没有任何可见的登录功能,我正在努力解决如何用我当前的logincontroller覆盖它。 在我的路线中,我只是调用LoginController @ login。并且我没有覆盖被调用的登录函数..

这是我的logincontroller:

namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;

use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Lang;
class LoginController extends Controller
{

    use AuthenticatesUsers;
    protected $redirectTo = '/minside';
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }
    protected function sendLoginResponse(Request $request)
    {
        $request->session()->regenerate();
        $this->clearLoginAttempts($request);
        if ($request->ajax()) {
            error_log($this->guard()->user(),'0');
            return response()->json($this->guard()->user(), 200);
        }
        return $this->authenticated($request, $this->guard()->user())
            ?: redirect()->intended($this->redirectPath());
    }
    protected function sendFailedLoginResponse(Request $request)
    {
        if ($request->ajax()) {
            return response()->json([
                'error' => Lang::get('auth.failed')
            ], 401);
        }
        return redirect()->back()
            ->withInput($request->only($this->username(), 'remember'))
            ->withErrors([
                $this->username() => Lang::get('auth.failed'),
            ]);
    }
}

1 个答案:

答案 0 :(得分:1)

使用Laravel提供的LoginController类时,会检查:

protected function attemptLogin(Request $request)
{
    return $this->guard()->attempt(
        $this->credentials($request), $request->has('remember')
    );
}

只需在您的请求中加入“remember”即可记住用户登录信息。