Route type delete does not work in Laravel

Viewed 135

I have following route and works

Route::post("delete-role", [RoleApiController::class, "Remove"]);

I tested it through postman like this

http://localhost/delete-role?api_token=hcvhjbhjb12khjbjhc876

Now, if I change above route and convert to type delete..

Route::delete("delete-role/{role_id}", [RoleApiController::class, "Remove"]);

it does not work. I get below error. It seems to be the reason that the api_token is missing.

enter image description here

I get same error when trying to update route like below

Route::delete("delete-role/{role_id}/{api_token}", [RoleApiController::class, "Remove"]);
5 Answers

You have to set header of your request as:

Accept=application/json

in postman.

If you don't set the required header for api, Laravel Passport can't understand request as an API client and so it will redirect to a /login page for the web.

Or you can set a middleware to check it in code:

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);
}

You have an incomplete details. but I see few issues here.

  1. You seem to be using web routes for your API requests which is a bad set-up

  2. You do not have a route with login name.

based on the error you posted, your request seems to successfully destroyed the token and logged you out, then called the middleware App\Http\Middleware\Authenticate which supposed to redirect your request to login route which does not exist and the reason you are getting that error.

You can see from that Authenticate middleware it will supposed to redirect you to login route for unauthenticated request. thats why you need to use the api routes so you can handle the response manually

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string|null
     */
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('login');
        }
    }
}

Also, I'm not so sure about this, but the reason you are not getting the same issue with your POST request is probably because your POST request does not call the Authenticate middleware or whatever in your routes file or class function that calls the authenticate middleware.

But again, just use api routes if you don't want un-authenticated request to automatically redirect your request to login routes which does not exist in your application

The problem is that he doesn't define route ('login'), add in Exceptions/Handler.php

 $this->renderable(function (AuthenticationException $e, $request) {
        if ($request->is('api/*')) {
        return response()->json(['meassge' => 'Unauthenticated']);
          
        }
    });

Then you should use Passport Or Sanctum for auth with Api, Continue from here https://laravel.com/docs/9.x/passport

Probably, this thread could help you. Route [login] not defined

(OR)

You need to setup auth scaffolding to get login route defined.

Important: your project will be overridden if you setup auth scaffold.

So, I would only recommend doing this if it is a new project or a testing app.

See this for detail doc but install Laravel Breeze would be suffice.

It Appears you have called route('login') without having defined it in your routes, Please remove route('login') from your code or define it in your routes. eg::

Route::get('login', [YourController::class, 'methodName'])->name('login');
Related