【发布时间】:2015-11-08 20:50:22
【问题描述】:
我正在使用 Laravel 5.1 并尝试实现用户身份验证。基本上没有登录就不能访问任何路线。
内置特征 (AuthenticatesAndRegistersUsers) 无法正常工作,因为在成功验证后,重定向控制器将无法通过 Auth::user() 或 $this->auth->user 或 $request->user() 拉出用户。
我确实在$this->auth->attempt() 之后通过 var-dumping 用户验证了这是一次成功的尝试。
但是使用return redirect()->intended(...) 重定向后,无法拉取用户。
我现在尝试通过跳过AuthenticatesAndRegistersUsers 并将其直接写入AuthController 来实现它,但再次遇到同样的问题。
这是AuthController 中进行验证的部分:
$this->validate($request, [
'email' => 'required|email',
'password' => 'required',
]);
$credentials = $request->only('email', 'password');
//attempt to authenticate
//if successful, redirect to the intended url or the default redirectPath
if ($this->auth->attempt($credentials, $request->has('remember')))
{
//when I do dd($this->auth->user()) here it shows the correct user record
return redirect()->intended($this->redirectPath);
}
它重定向到正确的 url (HomeController),但无法检索到用户(在 show 方法或任何其他方法中):
dd(Auth::user()); //null
dd(Auth::check());//null
HomeController 扩展了我的 BaseController,它使用了 auth 中间件
public function __construct()
{
$this->middleware('auth');
}
我有 Laravel 4 自定义身份验证,一切正常,我对 Laravel 5 中的中间件身份验证概念仍然很陌生。
任何帮助将不胜感激。
【问题讨论】:
-
Yani 你不需要像 Laravel 4 那样暗示,它已经内置了你可以使用的身份验证。我将发布资源填充