gpt4 book ai didi

php - 我已经使用 laravel passport 创建了 API 身份验证。当授权 token 出错时,它会向我发送错误 "Route [login] not defined"

转载 作者:可可西里 更新时间:2023-11-01 01:11:30 25 4
gpt4 key购买 nike

我已经使用 laravel passport 创建了 API 身份验证。当授权 token 出错时,它会向我发送错误“Route [login] not defined”,尽管我需要 JSON 响应,如“unauthorized 401”

这是 api.php 这里 users/authenticate 是一个登录路由,但是当我使用 auth:api 中的其他路由时中间件。如果 token 是错误的,它会向我发送一个错误“Route [login] not defined”,但我不需要这个错误。我需要像 {error:unauthorized , code:401} 这样的 JSON 错误。

<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::post('users/authenticate', ['uses' => 'Auth\LoginController@login']);






Route::group(['middleware' => ['auth:api']], function() {
/* Business details for New register business, get all business, update business, get single business */
Route::post('businesses/create', ['uses' => 'Business\BusinessController@businessRegister']);
Route::post('businesses/{id}', ['uses' => 'Business\BusinessController@businessUpdate']);
Route::get('businesses/{id}', ['uses' => 'Business\BusinessController@businessGet']);
Route::get('businesses-info/{id}', ['uses' => 'Business\BusinessController@businessInfoGet']);

});

这是auth.php

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

'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],

这是 AuthServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Laravel\Passport\Passport;

class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
];

/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();

Passport::routes();
}
}

这是 app.php

'providers' => [

/*
* Laravel Framework Service Providers...
*/
Illuminate\Auth\AuthServiceProvider::class,
Illuminate\Broadcasting\BroadcastServiceProvider::class,
Illuminate\Bus\BusServiceProvider::class,
Illuminate\Cache\CacheServiceProvider::class,
Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
Illuminate\Cookie\CookieServiceProvider::class,
Illuminate\Database\DatabaseServiceProvider::class,
Illuminate\Encryption\EncryptionServiceProvider::class,
Illuminate\Filesystem\FilesystemServiceProvider::class,
Illuminate\Foundation\Providers\FoundationServiceProvider::class,
Illuminate\Hashing\HashServiceProvider::class,
Illuminate\Mail\MailServiceProvider::class,
Illuminate\Notifications\NotificationServiceProvider::class,
Illuminate\Pagination\PaginationServiceProvider::class,
Illuminate\Pipeline\PipelineServiceProvider::class,
Illuminate\Queue\QueueServiceProvider::class,
Illuminate\Redis\RedisServiceProvider::class,
Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
Illuminate\Session\SessionServiceProvider::class,
Illuminate\Translation\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,

/*
* Package Service Providers...
*/

/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
Laravel\Passport\PassportServiceProvider::class,

],

这是 Handler.php

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Auth\AuthenticationException;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];

/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];

/**
* Report or log an exception.
*
* @param \Exception $exception
* @return void
*/
public function report(Exception $exception)
{
parent::report($exception);
}

/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
return parent::render($request, $exception);
}


}

最佳答案

我遇到了同样的问题,然后我在 web.php 的末尾添加了 Auth::routes(); 然后它开始工作了。

我不知道它是否正确,但它解决了问题。

另一个原因可能是你没有发送 laravel 所需的 header ,即

'accept' => 'application/json', //it tells server to send only json response
'content-type' => 'application/json' // it tells front-end app to send data in json format

希望能解决你的问题

关于php - 我已经使用 laravel passport 创建了 API 身份验证。当授权 token 出错时,它会向我发送错误 "Route [login] not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55115057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com