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

blackfenixuzb's avatar

message unauthenticated. laravel + React

Hello, I'm just learning these technologies. I wanted to make a student creation page. From the frontend. I am making a request to create

const {mutate: createStudent} = useMutation({
    mutationFn: async (data) => {
        const url = 'api/students';
        const response = await axios.post(url, data);
        return response.data;
    }});

If the receiving router is made open, it is considered unsafe. Therefore, I wanted to make sure that when sending a request the user logs into his account. The authentication itself was created using Laravel Breeze.

The router looks like this in api.php

Route::middleware('auth')->group(function () { Route::post('/students', [StudentController::class, 'store']); });

But when I send data to a post request I get an error even if I logged into my account {message: "Unauthenticated."} message:"Unauthenticated."

Maybe I didn’t take something into account when creating the request. Thank you in advance.

0 likes
2 replies
blackfenixuzb's avatar

I find solution after install composer require laravel/sanctum my Karnel.php was

'api' => [
            // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
            \Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

in first line EnsureFrontendRequestsAreStateful was commented, I remove comment and it's work now. But actually I don't know why it's important thing and what it do.

DhPandya's avatar

@blackfenixuzb It is one of the installation steps to make your frontend request Stateful.

https://laravel.com/docs/10.x/sanctum#installation

Next, if you plan to utilize Sanctum to authenticate a SPA, you should add Sanctum's middleware to your api middleware group within your application's app/Http/Kernel.php file:

'api' => [
    \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
    \Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
],

Please or to participate in this conversation.