Laravel 5 Override登录功能 [英] Laravel 5 Override Login Function
问题描述
我正在开发Laravel项目,并尝试从AuthenticatesAndRegistersUsers覆盖默认的postLogin().因此,我更新了AuthController并添加了它以覆盖内置登录名,
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,
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(),
]);
}
如您所见,我有$ result [0]-> status状态,该状态告诉用户是否已激活,如果未激活,则将其重定向回auth/login.我试图var_dump($ result [0]-> status);并且它工作正常,也意味着我覆盖它,因为它正在显示它,但是我的问题是与其将其重定向到auth/log,它仍然可以通过,即使状态为0也可以登录.似乎我是我的覆盖不起作用,但是当我var_dump $ result [0]-> status时,它会显示.我错过了什么吗?
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?
推荐答案
我会在postLogin()
函数中添加以下第一件事.
I would add following first thing in postLogin()
function.
$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');
}
状态是用户表中的标志. 0 =无效,1 =有效.因此整个功能如下所示.
status is a flag in user table. 0 = Inactive, 1 = active. so whole function would look like following..
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',
]);
}
这篇关于Laravel 5 Override登录功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!