nuxt-community / auth-module

Zero-boilerplate authentication support for Nuxt 2
https://auth.nuxtjs.org
MIT License
1.92k stars 924 forks source link

Set cookie domain to all subdomains by default not working as stated in the docs #1042

Open phips28 opened 3 years ago

phips28 commented 3 years ago

Version

module: ^4.9.1 nuxt: 2.14.5

In the docs (https://auth.nuxtjs.org/api/options#cookie) it says:

domain - domain (and by extension subdomain/s) where the cookie is visible. Default is domain and all subdomains.

But thats not true, its not default to all subdomains .domain.com instead it sets full domain www.domain.com.

What is expected?

set cookie on domain .domain.com by default, not the full domain

What is actually happening?

set cookie on www.domain.com

existe-deja commented 3 years ago

I had some extra test I made because I don't fully understand how to manage cookies.

I modified my hosts file with

127.0.0.1 localhost test.localhost

I ran two instances of the same projet via:

# term 1
$ yarn dev
# term 2
$ HOST=test.localhost PORT=2222 yarn dev

On both instances I can reach the app through localhost or test.localhost. I think HOST=test.localhost is not taken into consideration but anyway.

If I login in localhost:

If I login in test.localhost:

Once the right cookies are set, the auth reconstructs everything and works fine. But subdomains can't access to parent's cookies and vice versa.

So questions are:

viralpatel380 commented 2 years ago

Here is workaround - Create middleware and add in routeobject in nuxt.config.js

export default function ({$auth, $cookies}) {
    if($auth.loggedIn){
        const token = $auth.getToken('local')
        $cookies.set('lupower', token, {
            domain : '.letsupgrade.in'
          })
    } else if(!$auth.loggedIn){
        $cookies.set('lupower', false, {
            domain : '.letsupgrade.in'
          })
    }

}
R-Pin3da commented 2 years ago

Same issue here. I have to specify it for each environment.

cookie: { prefix: 'auth_', options: { expires: 365, domain: process.env.COOKIES_DOMAIN } }