Summer Sale! All accounts are 50% off this week.

AnthonyM's avatar

How to set a cookie on localhost subdomain

I'm working on an webapp (Laravel/Vue) with a subdomain per organisation companyone.mydomain.com, companytwo.mydomain.com, ...

As authentication system I'm using Laravel Sanctum with cookies. While setting a cookie is working on localhost it's not working when using subdomains locally.

My backend is running on localhost:8000 while the frontend is running on localhost:8080. For cors reasons I've added a proxy property in vue.config.js

module.exports = {
    devServer: {
        disableHostCheck: true,
        proxy: 'http://localhost:8000',
    },
};

GENERAL

I've changed my /etc/hosts file so I can use subdomains locally

127.0.0.1 localhost
127.0.0.1 companyone.mydomain.com
127.0.0.1 companytwo.mydomain.com

Laravel On the backend I've added the following lines to the .env file

SESSION_DOMAIN="mycompany.com"
SANCTUM_STATEFUL_DOMAINS="mycompany.com:8080"

In the Authcontroller I'm returning the cookie like this

$token = $user->createToken(Str::random(10))->plainTextToken;
$cookie = cookie('mydomain_api', $token, 60 * 24);
        
return response([
   'token' => $token,
   'user' => new AuthResource($user)
], 200)->withCookie($cookie);

The cookie settings are the following

$domain = null
$secure = true
$httpOnly = true
$sameSite = 'None'

When calling the login function I'm receiving the cookie in my browser but Application -> Cookies stays empty Set-Cookie: mydomain.api.com=115%7C67ztbGgV1snMr01IuoeWfWftanoPO9dZue90xr40; expires=Thu, 19-Aug-2021 07:14:50 GMT; Max-Age=86400; path=/; domain=localhost; secure; httponly; samesite=lax

I've also tried to hardcode the domain property of the cookie to mydomain.com but I'm getting the following error in the browser this attempt to set a cookie via set-cookie header was blocked because it's domain attribute was invalid regards to the current host url

How can I get the cookie stored in the browser cookie storage?

0 likes
0 replies

Please or to participate in this conversation.