【发布时间】:2023-03-05 21:09:01
【问题描述】:
有几个类似的问题,但似乎都不完整,因为它们指的是不存在的功能。
我指的是:
Check for active user state with laravel
Login only if user is active using Laravel
extend laravel 5 built-in authentication to login only "if user == active"
在所有这些解决方案中,都提供了主要用于更改 AuthController 中的功能的解决方案,但是这些功能不存在。
我使用的是最新版本的 Laravel (5.2),所以我提到的默认文件如下所示:https://github.com/laravel/laravel/blob/master/app/Http/Controllers/Auth/AuthController.php
现在,我该如何实现这个功能?我尝试将public function postLogin()(如其他提到的帖子中所建议的那样)复制到该AuthController 文件中。没有改变。
我显然在这里遗漏了一些东西。
请人帮忙!
编辑: 我添加的功能是:
public function postLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email', 'password' => 'required',
]);
$credentials = $this->getCredentials($request);
// This section is the only change
if (Auth::validate($credentials)) {
$user = Auth::getLastAttempted();
if ($user->active) {
Auth::login($user, $request->has('remember'));
return redirect()->intended($this->redirectPath());
} else {
return redirect($this->loginPath()) // Change this to redirect elsewhere
->withInput($request->only('email', 'remember'))
->withErrors([
'active' => 'You must be active to login.'
]);
}
}
return redirect($this->loginPath())
->withInput($request->only('email', 'remember'))
->withErrors([
'email' => $this->getFailedLoginMessage(),
]);
}
【问题讨论】:
-
如果您使用
Route::auth()为Auth 注册路由,则该方法不是postLogin而是login,请检查您的路由。 -
好的,我已将函数从
postLogin重命名为login。我有一个关于Request的异常,我必须添加use Illuminate\Http\Request。现在我得到另一个例外:Class 'App\Http\Controllers\Auth\Auth' not found -
这是一个 PHP 命名空间问题。
-
在此行触发:
if (Auth::validate($credentials)) {来自问题中编写的代码。好像我不能在这里使用门面。 -
这是一个命名空间问题。您必须指示 PHP 该类不在当前命名空间中。如果您需要对命名空间的简要介绍,请查看这篇文章:https://mattstauffer.co/blog/a-brief-introduction-to-php-namespacing
标签: php laravel authentication laravel-5