Php Lumen:如何使用中间件为用户进行身份验证登录?

Php Lumen:如何使用中间件为用户进行身份验证登录?,php,authentication,middleware,lumen,lumen-5.2,Php,Authentication,Middleware,Lumen,Lumen 5.2,我试图将用户名和密码从表单传递到控制器,检查该用户是否存在(users数据库表),然后重定向到一个经过身份验证的页面,只有登录的用户才能看到他们的用户名 在myroutes.php中: $app->post('/', ['uses' => 'AuthenticationController@login']); $app->group(['middleware' => 'auth'], function ($app) { $app->post('/ticke

我试图将用户名和密码从表单传递到控制器,检查该用户是否存在(users数据库表),然后重定向到一个经过身份验证的页面,只有登录的用户才能看到他们的用户名

在my
routes.php
中:

$app->post('/', ['uses' => 'AuthenticationController@login']);

$app->group(['middleware' => 'auth'], function ($app) {
    $app->post('/tickets', ['uses' => 'TicketsController@getTickets']);
});
然后我的
AuthenticationController.php
登录函数:

public function login(Request $request) {
    $credentials = $request->only(['username','password']);

    $user = App\User::find($credentials);

    if ($user) {
        // I'm assuming you do a redirect() here but am unsure how with Auth
    } else {
        return 'not logged in';
    }
}
此时,我不知道需要在ticketcontroller.php中的
getTickets
函数中放入什么

任何知道怎么做的人,请告诉我怎么做

最终,拥有
tickets.blade.php
视图,拥有
{{{user->username}}
并显示登录用户的用户名,这将非常棒。如果用户未登录并访问此视图,则不会显示任何内容,因为他们未经过身份验证

编辑:当前,我遇到以下错误:

AuthenticationController.php:类中的FatalErrorException 找不到“App\Http\Controllers\App\User”

在所有函数开始之前,我的
AuthenticationController.php
中确实包含了这两行代码:

use App\User;
use Auth;

也许这是我将
$credentials
传递到find函数的方式,但这并不能解决身份验证方面的问题。

首先,您发布的错误是名称空间问题。在find方法中删除
App\User
,只需拥有
User
。其次,这看起来像是Laravel身份验证,我很确定流明身份验证并不是这样工作的。请看@tam,我明白你的意思,但是你提到的文档使用Auth与API键相关联,在我的例子中,我不需要。只有用户信任通过中间件对用户进行身份验证。您找到了解决方案吗?@PradeepSapkota遗憾的是,仍然没有-但如果有人感兴趣,我恳求其他人开始悬赏:)如果您使用身份验证来保护
getTickets
,我建议您使用OAauth2。这是一个框架