laravel 如何在 Lumen 中使用身份验证进行用户登录?为什么我在启动时看到“未授权”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39022337/
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
How to use Authentication for user login in Lumen? Why do I see "Unauthorized" upon launch?
提问by LatentDenis
I have a login page (username/password inputs) that doesn't load/show. Instead, when launching the app, all that shows is "Unauthorized".
我有一个无法加载/显示的登录页面(用户名/密码输入)。相反,在启动应用程序时,所有显示的是“未经授权”。
This is from a command in Authenticate.php
that I have included further below.
这是来自Authenticate.php
我在下面进一步包含的命令。
My routes.php
:
我的routes.php
:
$app->get('/', 'PageController@index');
$app->group(['middleware' => 'middleware.auth'], function ($app) {
$app->post('/', ['uses' => 'AuthenticationController@login']);
});
My PageController.php
:
我的PageController.php
:
namespace App\Http\Controllers;
use App\User;
class PageController extends Controller
{
public function __construct()
{
//
}
public function index() {
return view('login');
}
}
My AuthenticationController.php
:
我的AuthenticationController.php
:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Auth;
class AuthenticationController extends Controller
{
public function __construct()
{
//
}
public function login(Request $request) {
$credentials = $request->only(['email','password']);
if (Auth::attempt($credentials, $request->has('remember'))) {
return'logged in';
} else {
return 'not logged in';
}
}
}
Here's Authenticate.php
located in 'app\Http\Middleware:
这里Authenticate.php
位于'app\Http\Middleware:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Auth\Factory as Auth;
class Authenticate
{
/**
* The authentication guard factory instance.
*
* @var \Illuminate\Contracts\Auth\Factory
*/
protected $auth;
/**
* Create a new middleware instance.
*
* @param \Illuminate\Contracts\Auth\Factory $auth
* @return void
*/
public function __construct(Auth $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if ($this->auth->guard($guard)->guest()) {
return response('Unauthorized.', 401);
}
return $next($request);
}
}
There might be a better way to go about what I'm trying to do, so if there is, please demonstrate to me what that is.
可能有更好的方法来解决我正在尝试做的事情,所以如果有,请向我展示那是什么。
But why am I seeing the Unauthorized
when my app loads? How can I fix this?
但是为什么我会看到Unauthorized
我的应用程序加载时的信息?我怎样才能解决这个问题?
回答by Matt Burgess
Looks like you're checking whether a user is authenticated before the AuthenticationController@login
method can be called. You need to remove the middleware auth from the post / route, as essentially what is happening is;
看起来您正在检查用户是否经过身份验证,然后AuthenticationController@login
才能调用该方法。您需要从帖子/路由中删除中间件身份验证,因为基本上正在发生的事情是;
- Homepage (
$app->get('/'...
) opens fine because there is no auth middleware defined for this route - When you post the login form, Lumen is told that only authenticated users can access that page because of the middleware.auth defined against your
POST /
route.
- 主页 (
$app->get('/'...
) 打开正常,因为没有为此路由定义身份验证中间件 - 当您发布登录表单时,Lumen 会被告知只有经过身份验证的用户才能访问该页面,因为针对您的
POST /
路由定义了 middleware.auth 。
This should work:
routes.php
这应该有效:
routes.php
$app->get('/', 'PageController@index');
$app->post('/', ['uses' => 'AuthenticationController@login']);
$app->group(['middleware' => 'middleware.auth'], function ($app) {
$app->get('/user/dashboard', ['uses' => 'Controller@method']);
});
With that, any one can see and submit your login page, but only logged in users can access the URL /user/dashboard
A. The rest of the code looks fine.
有了它,任何人都可以看到并提交您的登录页面,但只有登录用户才能访问 URL /user/dashboard
A。其余的代码看起来不错。