Search code examples
reactjslaravelhttp-status-code-401unauthorizedlaravel-sanctum

How do i fix 401 unauthorized error in react and laravel sanctum


hello I have a problem in react and laravel sanctum, it's running fine when I test on postman, but it showing an error 401 Unauthorized when I implementing it in react

i put this on my .env laravel file

SANCTUM_STATEFUL_DOMAINS=localhost:3000 SESSION_DRIVER=cookie SESSION_DOMAIN=localhost

this my stateful domain

'stateful' => explode(',', env(
    'SANCTUM_STATEFUL_DOMAINS',
    'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1,' . parse_url(env('APP_URL'), PHP_URL_HOST),
)),

here's my react front end

export const postLogout = (token) => (dispatch) => {
const config = {
  headers: { Authorization: "Bearer " + token },
};

console.log(config);
 axios.defaults.withCredentials = true;
 axios.get("http://localhost:8000/sanctum/csrf-cookie").then((response) => {
axios
  .post(`${Api}/logout`, config)
  .then((res) => {
    console.log(res);
    dispatch({
      type: POST_LOGOUT,
      payload: res,
    });
  })
  .catch((err) => {
    console.log(err);
  });
 });
};  




Solution

  • i found the bug, i try to delete something with post method, so I just need to change post to delete method