Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth across multiple subdomains #304

Open
CptJJ opened this issue Nov 9, 2023 · 8 comments
Open

Auth across multiple subdomains #304

CptJJ opened this issue Nov 9, 2023 · 8 comments
Labels
question Further information is requested

Comments

@CptJJ
Copy link

CptJJ commented Nov 9, 2023

Hello, I would like to make my auth sync across multiple subdomains domain.com -> a.domain.com

Similarly to
https://github.com/orgs/supabase/discussions/5742

Is there a convenient way to handle this via the nuxt module?

@CptJJ CptJJ added the question Further information is requested label Nov 9, 2023
@titusdecali
Copy link

I have the same question. Would be nice to have an answer in 2024

@felixgabler
Copy link
Contributor

felixgabler commented Apr 14, 2024

Hi, we are solving this by passing the parent domain as the domain in the cookie options

supabase: {
    redirect: false,
    cookieOptions: {
        domain: process.env.NUXT_PUBLIC_COOKIE_DOMAIN,
    },
},

@MalachiDraven
Copy link

Hi, we are solving this by passing the parent domain as the domain in the cookie options

supabase: {
    redirect: false,
    cookieOptions: {
        domain: process.env.NUXT_PUBLIC_COOKIE_DOMAIN,
    },
},

Can you elaborate a little more on that? I've tried everything I can think of and I can't get it to work across subdomains. It still sets a value in localStorage, which prevents it from working across subdomains. How exactly are you signing the user in and then how are you getting the user value?

@felixgabler
Copy link
Contributor

For us, it just works by passing example.com as the NUXT_PUBLIC_COOKIE_DOMAIN. Then, we can sign in on sub1.example.com and it recognizes our user on sub2.example.com. We are signing them in using useSupabaseClient<Database>().auth.signInWithOAuth and useSupabaseClient<Database>().auth.signInWithPassword.Then, we use useSupabaseUser to get the user data.

@MalachiDraven
Copy link

For us, it just works by passing example.com as the NUXT_PUBLIC_COOKIE_DOMAIN. Then, we can sign in on sub1.example.com and it recognizes our user on sub2.example.com. We are signing them in using useSupabaseClient<Database>().auth.signInWithOAuth and useSupabaseClient<Database>().auth.signInWithPassword.Then, we use useSupabaseUser to get the user data.

Hmm, weird. I've tried doing the same exact thing. It does correctly recognize the access_token and refresh_token cookies for example.com, but there's ALSO a value saved to localStorage that the useSupabaseUser() reads from which messes everything up. For example, if you log into sub1.example.com, and then visit sub2.example.com, it'll show you as logged in which is correct - but then try logging out on either subdomain and the localStorage on the other domain won't be deleted so it'll start giving errors about the auth session not being found or something.

@felixgabler
Copy link
Contributor

Hmm, weird. I've tried doing the same exact thing. It does correctly recognize the access_token and refresh_token cookies for example.com, but there's ALSO a value saved to localStorage that the useSupabaseUser() reads from which messes everything up. For example, if you log into sub1.example.com, and then visit sub2.example.com, it'll show you as logged in which is correct - but then try logging out on either subdomain and the localStorage on the other domain won't be deleted so it'll start giving errors about the auth session not being found or something.

I just tested this on our product and it correctly logs out across domains. I have to note that we are using an unpublished version from my PR #357. Wanna try it out too and see if it works there? @supabase/ssr does handle storing auth tokens a bit differently than this library originally did, I believe.

@MalachiDraven
Copy link

Hmm, weird. I've tried doing the same exact thing. It does correctly recognize the access_token and refresh_token cookies for example.com, but there's ALSO a value saved to localStorage that the useSupabaseUser() reads from which messes everything up. For example, if you log into sub1.example.com, and then visit sub2.example.com, it'll show you as logged in which is correct - but then try logging out on either subdomain and the localStorage on the other domain won't be deleted so it'll start giving errors about the auth session not being found or something.

I just tested this on our product and it correctly logs out across domains. I have to note that we are using an unpublished version from my PR #357. Wanna try it out too and see if it works there? @supabase/ssr does handle storing auth tokens a bit differently than this library originally did, I believe.

Oh yeah, that's definitely gotta be the solution! How can I use your version?

@felixgabler
Copy link
Contributor

Oh yeah, that's definitely gotta be the solution! How can I use your version?

It is not easy at the moment, I had to use a git submodule and put this into nuxt.config.ts

export default defineNuxtConfig({
    modules: [
        './modules/supabase-nuxt/src/module.ts',
        ...
    ], ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants