You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Storage: window.localstorage in createBrowserClient doesn't work #142
I've been pulling my hair out because I want our app to authenticate users when loaded as iframe from a different source. Cookies seem to have a permission problem when trying to set them from a diff. domain so I thought i'd use localstorage for storing sessions instead:
The default localStorage in the auth-js repo does, yes. But after looking at the ssr source code again, it ignores any custom storage you pass it. So, even if you did give it a proper implementation, it won't work. If you don't provide a cookies implementation, it defaults to using the document.cookie api and still stores things in cookies.
So, you'd need to use @supabase/supabase-js in order to get localStorage working. And in that case, there's no reason to use the ssr library.
import{createClientascreateSupabaseClient}from"@supabase/supabase-js";importtype{SupabaseClient}from"@supabase/supabase-js";importtype{Database}from"../../types/supabase";exportfunctioncreateClient(): SupabaseClient<Database>{// chekc if is embedreturncreateSupabaseClient<Database>(process.env.NEXT_PUBLIC_SUPABASE_URLasstring,process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEYasstring,{auth: {// detect if running in embedstorage: typeofwindow!=="undefined"&&window.self!==window.top ? window.localStorage : undefined,autoRefreshToken: true,persistSession: true,detectSessionInUrl: true,},});}
If you want to use localStorage with the @supabase/supabase-js clients, don't pass any of those options. If you don't pass storage it defaults to localStorage; and all the others you stated are the default values.
I'm confused, If by design why accept storage in createBrowserClient if it doesn't do anything?
So if I understand correctly there is no way for me to use @supabase/ssr with anything else than cookie storage? So no way to use auth from inside an Iframe (that blocks third party cookies)?
There is no way to use localStorage directly with the ssr library, correct. But, you shouldn't need the ssr library if you're not using cookie storage, so just use the supabase clients from supabase-js to achieve what you want. You won't be able to use a server client to access the user's session though, since it obviously won't have access to the browser's localStorage - unless you somehow pass that to the server yourself.
ssr is accepting storage from a typescript perspective, because in the code they're using SupabaseClientOptions as part of the type union, which would include storage. (The ssr library does use other options that a dev might pass in). Perhaps they should consider doing an omit to filter out options that are a no-op.
This is expected behavior, not a bug. @supabase/ssr always stores the session in cookies because that's how the server side client reads it, so any custom storage you pass in gets overridden. The types currently let you pass storage without warning you it's ignored, which is the confusing part, and we'll fix that separately.
For your use case, since you don't need the server to see the session, you don't need @supabase/ssr. Use @supabase/supabase-js's createClient directly with storage: window.localStorage, as you already found.
Activity
j4crev commentedon Nov 13, 2025
For custom storage, you have to implement certain methods. If you look around the source code, you'll see what I mean.
It's not documented well, as far as I recall.
tzvc commentedon Nov 14, 2025
@j4w8n Doesn't the localstorage class already implement all of them (getValue, setValue, etc...) ?
j4crev commentedon Nov 14, 2025
@tzvc
The default localStorage in the auth-js repo does, yes. But after looking at the ssr source code again, it ignores any custom
storageyou pass it. So, even if you did give it a proper implementation, it won't work. If you don't provide acookiesimplementation, it defaults to using thedocument.cookieapi and still stores things in cookies.So, you'd need to use @supabase/supabase-js in order to get localStorage working. And in that case, there's no reason to use the ssr library.
tzvc commentedon Nov 14, 2025
@j4w8n Is there a reason why storage is ignored in
createBrowserClient? Or is it just a bug?So if want to store session in localstorage in my iframe I just have to use the "legacy" client switching from this:
To this?
j4crev commentedon Nov 14, 2025
It's not a bug, but by design. I think you used to be able to pass your own storage to the ssr clients though.
This code passes your options, including
storage...but
createStorageFromOptionsdoesn't usestorage:If you want to use localStorage with the @supabase/supabase-js clients, don't pass any of those options. If you don't pass
storageit defaults to localStorage; and all the others you stated are the default values.tzvc commentedon Nov 14, 2025
I'm confused, If by design why accept
storageincreateBrowserClientif it doesn't do anything?So if I understand correctly there is no way for me to use
@supabase/ssrwith anything else than cookie storage? So no way to use auth from inside an Iframe (that blocks third party cookies)?j4crev commentedon Nov 14, 2025
There is no way to use localStorage directly with the ssr library, correct. But, you shouldn't need the ssr library if you're not using cookie storage, so just use the supabase clients from supabase-js to achieve what you want. You won't be able to use a server client to access the user's session though, since it obviously won't have access to the browser's localStorage - unless you somehow pass that to the server yourself.
ssr is accepting
storagefrom a typescript perspective, because in the code they're usingSupabaseClientOptionsas part of the type union, which would includestorage. (The ssr library does use other options that a dev might pass in). Perhaps they should consider doing anomitto filter out options that are a no-op.However, they overwrite any passed-in
storagewith their ownstoragebuilt from thecreateStorageFromOptionscode I referenced above.mandarini commentedon Aug 14, 2026
Hi @tzvc, thank you for opening this issue! 💚
This is expected behavior, not a bug.
@supabase/ssralways stores the session in cookies because that's how the server side client reads it, so any customstorageyou pass in gets overridden. The types currently let you passstoragewithout warning you it's ignored, which is the confusing part, and we'll fix that separately.For your use case, since you don't need the server to see the session, you don't need
@supabase/ssr. Use@supabase/supabase-js'screateClientdirectly withstorage: window.localStorage, as you already found.Thanks for the clear writeup here.
fix: warn when auth.storage is ignored by createBrowserClient/createS…
chore(main): release 0.12.5 (#286)