404 Sanctum Error

Sanctum is a great Laravel package for authenticating APIs. Typically, when working with it for API authentication the error you would most likely expect to see is the usual 401 Unauthorised and right away you know where to look. However, in a very unlikely situation I experienced a 404 page not found error. What even tripped me about this error was that once I took out my routes from the Sanctum guard it begun to work as expected. Of course, I did a Google search to see if anyone had come by this error before but my search was fruitless. Making me almost believe that my error was novel. Below was my sample code.

Route::middleware('auth:sanctum', function (){

    Route::resource('companies', CompaniesController::class);

});

It took another pair of developer eyes for me to realize my mistake, you probably have seen what I did wrong but my eyes were just too clouded to see it after having debugged for more than day.

Route::middleware('auth:sanctum')->group(function (){

    Route::resources([
        'companies' => CompaniesController::class,

    ]);

});

If you didn't see the mistake I made don't worry. I am going to explain it anyways. Instead of chaining my middleware function with a group function like the immediate code above, I rather used a closure function. How did I make such a mistake you say? Well, I was editing the default user route that comes with the routes file and for some strange reasons I ended up that way. I hope you don't fall into such an error but even if you do you have this article to point you to some hot spots. In case you have experienced other types of errors that result in 404 while using Sanctum feel free to share with me on Twitter