Laravel 5.4 Auth API 路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44311357/
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 16:04:37  来源:igfitidea点击:

Laravel 5.4 Auth API route

phplaravelapiauthenticationlaravel-5

提问by Gabriel Alejandro

I'm trying to build an API authentication, I defined a controller for it and there's another one for the web login. I configured 2 different auth in config/api, because there are 2 types of users, the ones that are going to be using the API and the other are the web users. This is the guards config:

我正在尝试构建 API 身份验证,我为它定义了一个控制器,还有另一个用于 Web 登录。我在config/api 中配置了两种不同的身份验证,因为有两种类型的用户,一种是将要使用 API 的,另一种是 Web 用户。这是守卫配置:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
    'users-api' => [
        'driver' => 'token',
        'provider' => 'users-api',
    ],
    'users' => [
        'driver' => 'session',
        'provider' => 'usuarios',
    ],
],

Thing is that whenever I try to access /api/loginI get redirected to /login (which is the web view, this is how I defined the Route in the api.php routes file:

事实是,每当我尝试访问/api/login 时,我都会被重定向到 /login(这是 Web 视图,这就是我在 api.php 路由文件中定义路由的方式:

Route::group(['middleware' => ['auth:users-api']], function () {
     Route::get('login','api\AuthenticationController@authenticate');
     Route::get('register','api\AuthenticationController@register');
});

It happens when I use the middleware, I have tried calling it from the Controller construct like this:$this->middleware('auth:users-api');

当我使用中间件时会发生这种情况,我尝试从 Controller 构造中调用它,如下所示:$this->middleware('auth:users-api');

But I still get redirected, if I comment that line or if I don't use the middleware in the routes file, I don't get redirected but I need it to specify which table the auth will use. If you need anything else to help me solve it, I'll gladly provide it. Thanks in advance.

但是我仍然被重定向,如果我注释该行或者如果我不在路由文件中使用中间件,我不会被重定向但我需要它来指定身份验证将使用哪个表。如果您需要其他任何东西来帮助我解决它,我很乐意提供。提前致谢。

采纳答案by Oscar Mu?oz

I found this tutorial very helpful, there are a lot of step to take into account when doing multiple Auths in one application

我发现本教程非常有用,在一个应用程序中进行多个身份验证时需要考虑很多步骤

https://ysk-override.com/blog/

https://ysk-override.com/blog/

hope it helps

希望能帮助到你