简体   繁体   English

Laravel 5.4 Auth API路由

[英]Laravel 5.4 Auth API route

I'm trying to build an API authentication, I defined a controller for it and there's another one for the web login. 我正在尝试构建API身份验证,为此我定义了一个控制器,并且还有另一个用于Web登录。 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. 我在config / api中配置了2种不同的auth,因为有2种类型的用户,将要使用API​​的类型是Web用户。 This is the guards config: 这是卫兵配置:

'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/login I 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的方式:

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. 提前致谢。

I found this tutorial very helpful, there are a lot of step to take into account when doing multiple Auths in one application 我发现本教程非常有帮助,在一个应用程序中执行多个Auth时要考虑很多步骤

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

hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM