【问题标题】:Laravel - Error when calling my logout routeLaravel - 调用我的注销路由时出错
【发布时间】:2021-03-21 05:19:32
【问题描述】:

我使用 auth 安装了基本身份验证指南发送的 laravel,我的登录工作正常并返回一个令牌,但是当我尝试注销时它返回以下错误:

HTTP/1.0 404 Not Found
Host: 127.0.0.1:8000
Date: Wed, 09 Dec 2020 19:33:08 GMT, Wed, 09 Dec 2020 19:33:08 GMT
Connection: close
X-Powered-By: PHP/7.2.24-0ubuntu0.18.04.7
Cache-Control: no-cache, private
Content-Type: application/json
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: X-Requested-With, Content-Type, X-Token-Auth, Authorization
    
{

  "message": "",
  "exception": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
  "file": "/home/shinier01/Projetos/Condivest/api-condinvest/api/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php",
"line": 43,
  "trace": [
   ...
   {
  "file": "/home/shinier01/Projetos/Condivest/api-condinvest/api/app/Http/Middleware/Cors.php",
  "line": 18,
  "function": "Illuminate\\Pipeline\\{closure}",
  "class": "Illuminate\\Pipeline\\Pipeline",
  "type": "->"
  },
 ...
  ]
}

我的中间件中的这个错误是他用令牌进行控制的地方,但我认为我做错了什么,它看起来像这样:

<?php

namespace App\Http\Middleware;

use Closure;

class Cors
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        return $next($request)->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
        ->header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, X-Token-Auth, Authorization');
    }
}

我的登出路线也是这样设置的:

Route::middleware('auth:api')->get('/user', function (Request $request) {
    Route::get('/logout', 'Auth\ApiAuthController@logout')->name('logout.api');
});

这样我就提出了我的要求:

GET http://127.0.0.1:8000/api/user/logout HTTP/1.1
Content-Type: application/json
Authorization: Bearer eyJ0eXA...6c

我已经尝试过放置“Bearer”并出现相同的错误。

有谁知道我做错了什么?还是我忘记安装的那个,要放入我的代码中?

感谢任何提示、帮助或回答。

【问题讨论】:

  • 您从未注册过您的logout.api 路线...您已在api/user 定义了一条路线,如果将其发送到该路线,则会注册一条名为logout.api 的路线...我想您想要@987654328 @ 而不是 get

标签: php laravel api authentication


【解决方案1】:

我的错误在于我如何在api中组装路由,请求中的路由都是正确的:

Route::group(['middleware' => ['cors', 'json.response', 'auth:api']], function () {
    Route::get('/auth/logout', 'Auth\ApiAuthController@logout')->name('logout.api');

    Route::post('/group/store', 'GroupController@store')->name('create.group.api');
});

请求:

GET http://127.0.0.1:8000/api/auth/logout HTTP/1.1
Content-Type: application/json
Authorization: Bearer eyJ...dulTQe7LfH26c

【讨论】:

    猜你喜欢
    • 2022-01-08
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    相关资源
    最近更新 更多