supabase
supabase copied to clipboard
Auth across multiple subdomains
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?
I have the same question. Would be nice to have an answer in 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,
},
},
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?
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.
For us, it just works by passing
example.com
as theNUXT_PUBLIC_COOKIE_DOMAIN
. Then, we can sign in onsub1.example.com
and it recognizes our user onsub2.example.com
. We are signing them in usinguseSupabaseClient<Database>().auth.signInWithOAuth
anduseSupabaseClient<Database>().auth.signInWithPassword
.Then, we useuseSupabaseUser
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.
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 theuseSupabaseUser()
reads from which messes everything up. For example, if you log intosub1.example.com
, and then visitsub2.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.
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 theuseSupabaseUser()
reads from which messes everything up. For example, if you log intosub1.example.com
, and then visitsub2.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?
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',
...
], ...