Laravel 8 with Sanctum Authentication (Reactjs SPA)

I am trying to learn Laravel sanctum,
I am stuck at the CSRF section, after changing the cors.php config value of ‘supports_credentials’ into true. I’m successfully getting a 204 response when connecting /sanctum/csrf-cookie. But after that when trying to connect to ‘/login’, I am getting error 419 unknown status.

I have added this to my env file

SESSION_DOMAIN=localhost:8000 // Laravel backend
SANCTUM_STATEFUL_DOMAINS=http://127.0.0.1:3000 // React Fronend

Here is my Cors.php

'paths' => ['api/*', 'sanctum/csrf-cookie'],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => true,

And here’s my **React Code **

const handleSubmit = (e) => {
    e.preventDefault();

    apiClient.get("sanctum/csrf-cookie").then((response) => {
      apiClient
        .post(
          "api/login",{
            email: email,
            password: password,
          }).then((response) => {
              console.log(response);
           });
       });
  };

Below is apiClient’s Code

import axios from "axios";

const apiClient = axios.create({
  baseURL: "http://127.0.0.1:8000/",
  withCredentials: true
});

export default apiClient;

API’s are working completely fine on Postman

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You try command. Php artisan optimize:clear


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x