Laravel Passport Route [login] not defined

こ雲淡風輕ζ 提交于 2019-12-10 13:09:31

问题


i use Laravel passport for auth

in route api.php

Route::get('/todos', function(){
  return 'hello';
})->middleware('auth:api');

but when open localhost:8000/api/todos I see the following error

 InvalidArgumentException
Route [login] not defined.
 * @return string
 *
 * @throws \InvalidArgumentException
 */
public function route($name, $parameters = [], $absolute = true)
{
    if (! is_null($route = $this->routes->getByName($name))) {
        return $this->toRoute($route, $parameters, $absolute);
    }

    throw new InvalidArgumentException("Route [{$name}] not defined.");
}

/**
 * Get the URL for a given route instance.
 *
 * @param  \Illuminate\Routing\Route  $route
 * @param  mixed  $parameters
 * @param  bool   $absolute
 * @return string
 *
 * @throws \Illuminate\Routing\Exceptions\UrlGenerationException
 */
protected function toRoute($route, $parameters, $absolute)
{
    return $this->routeUrl()->to(
        $route, $this->formatParameters($p

I want if the user was not authenticated Do not redirect to any page and only see the page


回答1:


Did you enter above-mentioned URL directly in browser search bar? If you did its wrong way because you also need to enter API token with your request__!!

To check either request includes token or not make your own middleware.

Command to create Middleware

php artisan make:middleware CheckApiToken

https://laravel.com/docs/5.6/middleware

change middleware handle method to

public function handle($request, Closure $next)
{
    if(!empty(trim($request->input('api_token')))){

        $is_exists = User::where('id' , Auth::guard('api')->id())->exists();
        if($is_exists){
            return $next($request);
        }
    }
        return response()->json('Invalid Token', 401);
}

Like This Your Url should be like this

http://localhost:8000/api/todos?api_token=API_TOKEN_HERE




回答2:


Use Postman and set the Header Accept: application/json otherwise Laravel Passport would never know it's an API client and thus redirect to a /login page for the web.




回答3:


You also have to add another header Key: Accept and value: application/json




回答4:


Check Your Header Request to put

Authorization = Bearer {your token}




回答5:


You also have to add another header Key: X-Requested-With and value: XMLHttpRequest




回答6:


In the following of @Eki answer,

This error is because you didn't set "Accept" field in your headers.

To avoid this error, add a middleware with priority to Authenticate to check that:

  1. add an extra middleware with below handler

    public function handle($request, Closure $next)
    {
        if(!in_array($request->headers->get('accept'), ['application/json', 'Application/Json']))
            return response()->json(['message' => 'Unauthenticated.'], 401);
    
        return $next($request);
    }
    
  2. set priority in app/Http/Kernel.php

    protected $middlewarePriority = [
        ...
        \App\Http\Middleware\MyMiddleware::class, // new middleware
        \App\Http\Middleware\Authenticate::class,
        ...
    ];
    
  3. add new middleware to your route

    Route::get('/todos', function(){
        return 'hello';
    })->middleware('MyMiddleware', 'auth:api');
    


来源:https://stackoverflow.com/questions/49294253/laravel-passport-route-login-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!