First, go to your Laravel app and find the file cors.php

app→config→cors.php and set it to like below

'supports_credentials' => true,

And most important if you are working on Localhost then go to the .env file and  paste the below code.

SESSION_DOMAIN=http://127.0.0.1:3000

SANCTUM_STATEFUL_DOMAINS=http://127.0.0.1:3000

Now go to your React app and then open the file where you called axios.

axios.get('/sanctum/csrf-cookie').then(response => {

        axios.post(`api/register`, data).then(res => {

        });

        });

axios.get('/sanctum/csrf-cookie').then(response => }) will generate csrf token.

Then go to your main file App.js and paste below code

axios.defaults.baseURL="http://127.0.0.1:8000/";

axios.defaults.headers.post['Content-Type']='application/json';

axios.defaults.headers.post['Accept']='application/json';

axios.defaults.withCredentials = true;

axios.defaults.baseURL="http://127.0.0.1:8000/";  you can leave this line.

Thanks for reading if any query comment me.

Leave a Comment
Advik Comment on: 18-11-2023 11:11:57

This blog really help me. Thanks