I'm using the artisan make:auth.
The session authentication works fine, but the ajax api requests gives me 401 Unauthorized (using Vue).
I can see the X-CSRF-TOKEN is set in the header.
And if I remove the 'middleware' => ['auth:api'] the $request->header('X-CSRF-TOKEN') is also set.
Any ideas?
Or suggestion on how to debug this further?
@subpublic Have you added the '\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,' to your Kernel.php file within the 'web' middleware group?
Hi there. I'm having the same problem and this is bugging me.
Do I have to install Passport (a tool to ease the OAuth Server creation) just so I can use my API via JavaScript?
Shouldn't it be easier? I want to use the API via JS only when I am authenticated. How can I accomplish that?
@jfvoliveira At last I found out the problem.
If you only want to make simple Ajax requests, don't put those urls in the /api.
You can just use middleware('web') and respond in json.
@subpublic I've tried that but if I include the auth middleware (I need it as I need to know what user did what action) I keep getting the TokenMismatchException.
@jfvoliveira The auth middleware works fine for me, as long as I keep the routes in web.php.
But I did have some token trouble, but couldn't really understand if it was bacause I had installed the auth:api or if I had moved around to many files. So I set up a brand new Laravel without auth:api and now it works just fine.