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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:20:05  来源:igfitidea点击:

How to use Authentication for user login in Lumen? Why do I see "Unauthorized" upon launch?

phplaravelauthenticationmiddlewarelumen

提问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.phpthat 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.phplocated 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 Unauthorizedwhen 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@loginmethod 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/dashboardA. The rest of the code looks fine.

有了它,任何人都可以看到并提交您的登录页面,但只有登录用户才能访问 URL /user/dashboardA。其余的代码看起来不错。