如何在Lumen中使用身份验证进行用户登录?为什么我会看到“未经授权"在发射时? [英] How to use Authentication for user login in Lumen? Why do I see "Unauthorized" upon launch?

查看:68
本文介绍了如何在Lumen中使用身份验证进行用户登录?为什么我会看到“未经授权"在发射时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无法加载/显示的登录页面(用户名/密码输入).相反,在启动应用程序时,所有显示的都是未经授权".

I have a login page (username/password inputs) that doesn't load/show. Instead, when launching the app, all that shows is "Unauthorized".

这来自Authenticate.php中的命令,我将在下面进一步介绍.

This is from a command in Authenticate.php that I have included further below.

我的routes.php:

$app->get('/', 'PageController@index');

$app->group(['middleware' => 'middleware.auth'], function ($app) {
    $app->post('/', ['uses' => 'AuthenticationController@login']);
});

我的PageController.php:

namespace App\Http\Controllers;

use App\User;

class PageController extends Controller
{
    public function __construct()
    {
        //
    }

    public function index() {

        return view('login');
    }
}

我的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';
        }
    }
}

这里的Authenticate.php位于'app \ Http \ Middleware:

Here's Authenticate.php located in '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.

但是为什么我的应用加载时会看到Unauthorized?我该如何解决?

But why am I seeing the Unauthorized when my app loads? How can I fix this?

推荐答案

就像您要在调用AuthenticationController@login方法之前检查用户是否已通过身份验证.您需要从发布/路由中删除中间件auth,因为本质上是这样;

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;

  • 首页($app->get('/'...)可以正常打开,因为没有为此路由定义身份验证中间件
  • 发布登录表单时,Lumen会被告知,由于针对您的POST /路由定义了middleware.auth,因此只有经过身份验证的用户才能访问该页面.
  • 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.

这应该起作用: 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']);
});

这样,任何人都可以看到并提交您的登录页面,但是只有登录的用户才能访问URL /user/dashboard A.其余代码看起来不错.

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.

这篇关于如何在Lumen中使用身份验证进行用户登录?为什么我会看到“未经授权"在发射时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆