Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alex_storm's avatar

Invalid credentials. Auth.basic fails (laravel 5.5)

Hello! I'm trying to create simple http basic authentication. For this I use the example given on the official website - https://laravel.com/docs/5.5/authentication

I created route

Route::post('api/post')->middleware('auth.basic');
At the outset, the error was due to the lack of a csrf_token, I added it, but... don't working( i have error - The page has expired due to inactivity. Please refresh and try again

OK, I fixed this problem by adding a router to an exception, but i see new problem - Invalid credentials..

I don't understand what i do wrong

this is simple code API curl:

curl_setopt($ch, CURLOPT_URL, 'http://server.io/api/post');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $params);

Where is params =
$params = [
            'email'=>'[email protected]',
            'password'=>'[email protected]',
            '_token'=>csrf_token(),
];

I can not understand how I can debug these errors, it's simple authentication without customization ...

I tested the standard authentication of Laravel, it does not work either ...

I'm working in a local environment: OpenServer (PHP 7.1, Apache + Nginx x64, MySQL 5.6, Redis 3.2)

Thanks!

0 likes
3 replies
Snapey's avatar

I don't think you can use auth.basic in this case. You need to create the middleware which calls the onceBasic method.

the auth.basic middleware in a route causes your browser to throw up a dialog box asking for credentials.

As you are passing credentials in the headers, I assume your use is for an API

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

Also, is the password held in the database hashed with bcrypt?

alex_storm's avatar
alex_storm
OP
Best Answer
Level 3

Yes, I understand this and I do according to the documentation. The password is transmitted correctly.

I solved the problem by reinstalling laravel.

I do not know what the problem was, but basic authentication from the box did not work when the project was first installed

2 likes

Please or to participate in this conversation.