-
-
Notifications
You must be signed in to change notification settings - Fork 168
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
Inconsistent behaviour of auth in Supabase Client between tab/popup and service-worker #456
Comments
Hey @gprieto, glad to hear there are more extension developers using supabase. I've only had experience with Google and MagicLink, but I would think the auth flow should be the same for any of these providers. So with Google you get the provider_token, but that's passed to a supabase auth redirect which then redirects to your website. I might have overcomplicated this process but here's what I have for my extension:
So, you're using v1. In v2, things change. You might have seen In v2, things will just work regardless of what area of the extension you're running a supabase request, popup, content_script, or background. It works because supabase fetch checks auth session for a valid accessToken. In v1, things don't work as you'd expect. First of all My recommendation is to upgrade to v2. There are still a few gotchas, you can see my journey in this PR I submitted: #444. I've since reworked my strategy, so it should help you too. One thing I still have to work out is how to sync the session between the web app and the extension. Right now the web app is just a facade for authentication, which relays the token to the extension, but in the future I want them to work in conjunction. So that'll be a tricky thing to work through when I get to it. I put some thought, and the best I could come up with is to share the session through cookies. That's probably what I should have done since day 1, but I'm going with localStorage for now. I'm sure I can migrate that from chrome.storage.local to chrome.cookies. So just an FYI |
Hey @miguelespinoza, I appreciate your detailed answer, many thanks! I really hope that v2 will solve this, I will look at your PR more in-depth in the coming days when I find the time. Regarding your process:I found it is much simpler to launch the Supabase log-in process with a provider directly from the extension, with a redirect to the extension. A couple of cave-heats on that:
I just found a working fix for keeping the service-worker logged in (with v1)The trick is to call a
const setSupabaseSession = async (config: ChromeConfig) => {
if (config['supabase.auth.token']) {
const session = config['supabase.auth.token'].currentSession;
if (
session.expires_at &&
session.refresh_token &&
(isPast(session.expires_at) || !supabase.auth.session())
) {
// If no session or token has expired, getSession using the refresh_token
console.log('Getting new Supabase session - refreshing Token');
const { session: newSessionData, error: newSessionError } = await supabase.auth.setSession(
session.refresh_token
);
if (newSessionError) throw newSessionError;
// Write back the session, with the new refresh_token, to localStorage
chrome.storage.local.set({ 'supabase.auth.token': { currentSession: newSessionData } });
} else console.log('Supabase session valid');
} else {
throw new Error('No Supabase session in config');
}
}; |
are you calling setSupabaseSession with what you store in chrome.storage.local? Doesn't this setup result in updating refresh_tokens on every function call. setSession will call the api.. |
Update: it seems that v2 solves this problem - and the session is properly written to and read from the specified storage. |
fwiw, i think local storage is not accessible from the service worker based on the mdn docs |
FYI - Although from the Chrome Developpers docs, you do have access to the
Storage API inside a service worker (tested and it works):
https://developer.chrome.com/docs/extensions/mv3/migrating_to_service_workers/#workers
…On Wed, Oct 26, 2022, 16:00 Kang Ming ***@***.***> wrote:
fwiw, i think local storage is not accessible from the service worker
based on the mdn docs
<https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers#:~:text=Note%3A%20localStorage%20works%20in%20a,not%20allowed%20in%20service%20workers.>
—
Reply to this email directly, view it on GitHub
<#456 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEJSZPLYNYNH7IPC6O4ZCBDWFE2QLANCNFSM6AAAAAAQWNJEQQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Bug report
Describe the bug
I am developing a chrome extension with auth using an external provider.
popup
, works fine: the session is stored inchrome.local.storage
.service-worker
script,supabase.auth.session()
returnsnull
Is there another way to implement this that I am not seeing?
supabase.ts:
Also both the
popup
and theservice-worker
implement theonAuthListener
.Expected behavior
As both the
popup
and theservice-worker
supabase clients share the same storage, they should both have the same session.Additionnal information
I have tried manually singing in from the
service-worker
with the following snippet:The sign in works and then the
refresh_token
becomes invalid but the updatedrefresh_token
is not persisted back tochrome.storage.local
so once thetoken
expires the user is logged out.The text was updated successfully, but these errors were encountered: