Laravel 5.4 + Ajax equals 401 Unauthenticated Laravel 5.4 + Ajax equals 401 Unauthenticated ajax ajax

Laravel 5.4 + Ajax equals 401 Unauthenticated


The point is you aren't authenticated. The CSRF Token isn't an authentication token.

You will need a way of authenticating your users against the api and (for example) give them an unique auth token, which they send with each request, in order to be sure they are allowed to use your API.

Maybe this link may be helpful:

https://laracasts.com/discuss/channels/laravel/53-api-routes-auth-middleware-confusion

This part of the docs maybe helpful too. It's about HTTP basic authentication:

https://laravel.com/docs/5.4/authentication#http-basic-authentication

Especially the part "Stateless HTTP Basic Authentication"


Let's take a look at the HomeController construct method. Is there a call that looks something like this?

$this->middleware(['auth']);


I have two solutions:

  1. Create a controller without using the auth middleware: $this->middleware(['auth']);

  2. Creating the route before the Route::group(['middleware' => 'auth'] in the routes/web.php file.

enter image description here