gpt4 book ai didi

php - Laravel 5 覆盖登录功能

转载 作者:搜寻专家 更新时间:2023-10-31 21:07:14 25 4
gpt4 key购买 nike

我正在处理我的 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(),
]);
}

如您所见,我有 $result[0]->status 告诉用户是否已激活,如果没有,我会将他们重定向回 auth/login。我试着 var_dump($result[0]->status);它工作正常,也意味着我重写了它,因为它正在显示它,但我的问题不是将它重定向到 auth/login,它仍在通过主页,即使状态为 0 也可以登录。看来我是我的override 不起作用,但是当我 var_dump $result[0]->status 时,它显示了。我错过了什么吗?

最佳答案

我会在 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');
}

状态是用户表中的一个标志。 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',
]);
}

关于php - Laravel 5 覆盖登录功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30483643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com