Laravel 5 覆盖登录功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30483643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 Override Login Function
提问by Hymanhammer013
I'm working on my Laravel Project and trying to override the default postLogin() from AuthenticatesAndRegistersUsers . So I have updated my AuthController and added this to override the built-in login,
我正在处理我的 Laravel 项目并尝试从 AuthenticatesAndRegistersUsers 覆盖默认的 postLogin() 。所以我更新了我的 AuthController 并添加了它来覆盖内置登录,
public function postLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email', 'password' => 'required',
]);
$credentials = $request->only('email', 'password');
if ($this->auth->attempt($credentials, $request->has('remember')))
{
/* Check if the user is Activated */
$userID = \Auth::user()->id;
$user = new \App\User;
$result = $user->isUserActivated($userID);
if($result[0]->status == 1)
{
return redirect()->intended($this->redirectPath());
}
else if($result[0]->status == 0)
{
Session::flash('alert-danger', 'Your account is not yet Activated.');
return Redirect::to('auth/login');
}
}
return redirect($this->loginPath())
->withInput($request->only('email', 'remember'))
->withErrors([
'email' => $this->getFailedLoginMessage(),
]);
}
As you can see I have $result[0]->status which tells if the user is activated, if not then I will redirect them back to auth/login. I tried to var_dump($result[0]->status); and it is working fine and also means I override that it coz it's displaying it but my problem is instead of redirecting it to auth/login it is still going thru home and can login even if the status is 0. Seems I'm it's my override doesn't work, but when I var_dump $result[0]->status, it shows. Did i missed something?
正如你所看到的,我有 $result[0]->status ,它告诉用户是否被激活,如果没有,那么我会将它们重定向回 auth/login。我试图 var_dump($result[0]->status); 它工作正常,也意味着我覆盖它,因为它正在显示它,但我的问题是,而不是将它重定向到身份验证/登录,它仍然通过主页,即使状态为 0 也可以登录。似乎我是我的覆盖不起作用,但是当我 var_dump $result[0]->status 时,它会显示。我错过了什么吗?
采纳答案by pinkal vansia
I would add following first thing in postLogin()
function.
我会在postLogin()
函数中添加以下第一件事。
$this->validate($request, [
'email' => 'required|email', 'password' => 'required',
]);
if ($this->auth->validate(['email' => $request->email, 'password' => $request->password, 'status' => 0])) {
return redirect($this->loginPath())
->withInput($request->only('email', 'remember'))
->withErrors('Your account is Inactive or not verified');
}
status is a flag in user table. 0 = Inactive, 1 = active. so whole function would look like following..
status 是用户表中的一个标志。0 = 不活动,1 = 活动。所以整个函数看起来像下面..
public function postLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email', 'password' => 'required',
]);
if ($this->auth->validate(['email' => $request->email, 'password' => $request->password, 'status' => 0])) {
return redirect($this->loginPath())
->withInput($request->only('email', 'remember'))
->withErrors('Your account is Inactive or not verified');
}
$credentials = array('email' => $request->email, 'password' => $request->password);
if ($this->auth->attempt($credentials, $request->has('remember'))){
return redirect()->intended($this->redirectPath());
}
return redirect($this->loginPath())
->withInput($request->only('email', 'remember'))
->withErrors([
'email' => 'Incorrect email address or password',
]);
}
回答by Conor
Try following this discussion at laracasts.
尝试在laracasts 上关注此讨论。
Here is a solution
这是一个解决方案
if ($this->guard()->validate($this->credentials($request))) {
$user = $this->guard()->getLastAttempted();
if ($user->is_activated && $this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
} else {
$this->incrementLoginAttempts($request);
if ($request->ajax()) {
return response()->json([
'error' => 'This account is not activated.'
], 401);
}
}
}
It provided a crucial update for a do my homeworkservice at Top Homework Expert
它为Top Homework Expert的“做我的作业”服务提供了重要更新
回答by Szenis
With
和
if ($this->auth->attempt($credentials, $request->has('remember')))
you are loggin the user in so if you want to log him out use
您正在登录用户,因此如果您想将他注销,请使用
Auth::logout();
use that piece of code in the else if statement
在 else if 语句中使用那段代码