1
votes

i am new to laravel and laravel-passport, i was following this article on medium here

everything going as i exactly want, until i tried to handle the error if the user sent wrong authorization token in details method

here is the error:

InvalidArgumentException

Route [login] not defined.

i am using laravel 5.7 and passport 7.1 which is the latest

i've tried

first snippet

  • to try and catch the error in show method

second snippet

  • to handle the token mismatch exception in /app/exceptions/handler.php it wasn't a token mismatch exception i guess

third snippet

  • even to miss up a little with laravel core at Handler.php file

in the unauthenticated method but no hope

/routes/api.php

Route::get('user/login', 'AuthController@login');

// user
Route::get('users/find', 'UserController@index');

//here i am trying this route
//with wrong authorization tocken
Route::middleware('auth:api')->group(function () {
    Route::get('user', 'UserController@show');
});
Route::post('user', 'UserController@store');
Route::put('user/{id}', 'UserController@update');
Route::delete('user/{id}', 'UserController@destroy');

/UserController.php

public function show()
{

    if(Auth::check()){
        $user = auth()->user();
        return response()->json(['user'=>auth()->user()], 200);        
    }else{
        return response()->json("can't connect", 400);
    }

}

First Snippet public function show() {

    try {
        $user = auth()->user();
    } catch (\Throwable $th) {
        return response()->json("can't connect", 400);
    }

    return response()->json(['user'=>auth()->user()], 200);

}

Second Snippet /app/exceptions/handler.php

public function render($request, Exception $exception)
{
    if ($exception instanceof TokenMismatchException) {
        return "Token error";
    }

    return parent::render($request, $exception);
}

Third Snippet

\vendor\laravel\framework\src\Illuminate\Foundation\Exceptions\Handler.php

protected function unauthenticated($request, AuthenticationException $exception)
{
    return $request->expectsJson()
                ? response()->json(['message' => $exception->getMessage()], 401)
                : redirect()->guest($exception->redirectTo() ?? route('login'));
}

Error Snippet (might be useful)

public function route($name, $parameters = [], $absolute = true)
{
    if (! is_null($route = $this->routes->getByName($name))) {
        return $this->toRoute($route, $parameters, $absolute);
    }

    // the following line is highlighted 
    throw new InvalidArgumentException("Route [{$name}] not defined.");
}

i expect the return to be a json not this error template

cause you might figured out i am trying to build a restful API

1

1 Answers

1
votes

You can create a custom error and in render method add standard response you can return the json you want.

For example create a new exception App/Exceptions/CustomInvalidTokenException.php:

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Http\Request;

class CustomInvalidTokenException extends Exception
{
    /**
     * Render the exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request
     * @return \Illuminate\Http\Response
     */
    public function render(Request $request)
    {
        return response()->json([
            'error' => $this->getMessage(),
            'status' => 0
        ], 500);
    }
}

Then in your controller throw it and internally it will render the exception response for you :

<?php 


try{
    // something
}
catch(TokenMismatchException $e){

    throw new CustomInvalidTokenException('Invalid token found');
}

If you want to handle the exception gobally and not on specific route then you can do the throw new CustomInvalidTokenException inside handler.php