The issue you're encountering is related to how browsers handle cookies with domain attributes. When you set SESSION_DOMAIN to .test, it implies that the cookie should be valid for all subdomains of .test. However, .test is a top-level domain (TLD) used for testing purposes, and browsers may not allow setting cookies for TLDs directly due to security reasons.
To resolve this issue, you can try the following steps:
-
Use a Specific Subdomain: Instead of using
.test, specify a more precise domain that includes a subdomain. For example, if your application is running onexample.test, setSESSION_DOMAINto.example.test. This will allow cookies to be shared across subdomains likeapp.example.testorapi.example.test. -
Update Your
.envFile: Modify your.envfile to reflect the specific subdomain:SESSION_DOMAIN=.example.test -
Clear Configuration Cache: After making changes to your
.envfile, clear the configuration cache to ensure Laravel picks up the new settings:php artisan config:cache -
Check Your Cookie Settings: Ensure that your
config/session.phpfile has the correct settings for cookies. Specifically, check thedomainandsecureoptions:'domain' => env('SESSION_DOMAIN', null), 'secure' => env('SESSION_SECURE_COOKIE', false), -
Use a Local Development Environment: If you're using a local development environment like Laravel Valet or Homestead, ensure that your local DNS is set up correctly to handle subdomains.
By specifying a more precise domain, you should be able to share session cookies across subdomains without encountering the invalid domain error. If you continue to experience issues, double-check your local DNS setup and ensure that your browser's cookie settings are not overly restrictive.