我想了解如何在主题行中按要求执行操作,因为下面的代码工作正常,但用户在检查$ user->已激活状态之前已登录。以下是一些代码来说明:
AuthController
public function authenticated(Request $request, User $user)
{
if ($user->Activated) {
return redirect()->intended($this->redirectPath());
} else {
Auth::logout();
return redirect($this->loginPath())
->withInput($request->only('email', 'remember'))
->withErrors([
'activated' => 'You need to activate your account to login'
]);
}
}
最好我想做以下事情:
AuthController
public function getCredentials(Request $request)
{
$credentials = $request->only($this->loginUsername(), 'password');
return array_add($credentials, 'Activated', '1');
}
但是,返回的唯一消息是"这些凭据与我们的记录不匹配。"而不是"您需要激活您的帐户才能登录"。另外,一旦用户登录,我将如何更新LoginStatusId,目前我这样做:
AuthController
public function authenticated(Request $request, User $user)
{
if ($user->Activated) {
$user->LoginStatusId = 1;
$user->save();
return redirect()->intended($this->redirectPath());
} else {
Auth::logout();
return redirect($this->loginPath())
->withInput($request->only('email', 'remember'))
->withErrors([
'activated' => 'You need to activate your account to login'
]);
}
}
登录后是否有更好的地方设置$ user-> LoginStatusId,或者这是放置它的最佳位置?
答案 0 :(得分:0)
打开此文件vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php
在postLogin
// If activated is equal to 1, user allowed to login or throw an credential mismatch error
$userData = User::select('activated')->where('email', $request['email'])->first();
if ($userData->activated == 1) {
$request['activated'] = $activated;
}
$credentials = $this->getCredentials($request); //add these code before this line
并将'activated'
添加到getCredentials方法。它看起来像这样:
protected function getCredentials(Request $request)
{
return $request->only($this->loginUsername(), 'password', 'activated');
}
您可以使用此Auth::user()
在任意位置查看用户登录状态。无需自己存储登录状态。作为任何控制器中的示例,您可以这样写:
if(Auth::user()){
// do this;
}else{
// do that;
}