How to fix Laravel Sanctum 401 Unauthorized Error

1. The values of domains in “allowed_origins” array in cors.php config file should consist scheme, hostname and port number. No trailing slashes.
Example of wrong value: localhost:3000 or example.com
Example of correct value: http://localhost:3000 or http://example.com

2. Value of “SESSION_DOMAIN” in session.php file should not include the scheme, port, or a trailing slash.
Example of wrong value: http://localhost:3000 or http://example.com
Example of correct value: localhost or example.com

3. Not setting the cookie domain will cause cookies to be tied only to the top-level domain. Setting the cookie domain afterwards will lead to having two pairs of cookies – causing 401 responses.

4. The values of Stateful domains “SANCTUM_STATEFUL_DOMAINS” should only include the hostname and port (if specified).

5. Make sure the value “APP_URL” environment variable is correctly.

6. If nothing works then check the .htaccess file at location laravelproject/public/.htaccess and change the code of this file to default code that comes with laravel installation.