Hi <:vmathi:812484657547313212> I'm trying to use ...
# javascript
o
Hi I'm trying to use the provider way to login. Problem: it looks like the ``client.auth.user()`` is not reactive? Like I need to refresh my page to get the data (see gif)
Hi I'm trying to use the provider way to login. Problem: it looks like the ``client.auth.user()`` is not reactive? Like I need to refresh my page to get the data (see gif)
I don't really know if it comes from Supabase or me, here is my code:
Copy code
js
import { createClient } from '@supabase/supabase-js';

const client = createClient(
  import.meta.env.VITE_SUPABASE_URL as string,
  import.meta.env.VITE_SUPABASE_KEY as string
);

console.log('client.auth.user() =>', client.auth.user());
After some searchs, I think it could be come from there: https://github.com/supabase/gotrue-js/blob/master/src/GoTrueClient.ts#L67-L68
Okay so after tried a lot of things with gotrue, I get something really.... uh
Like I get the object in the localStorage, but when I'm trying to get it I get a null value??
h
you can listen to onAuthStateChange to receive notification whenever auth event happens https://supabase.io/docs/reference/javascript/auth-onauthstatechange
o
Oh this is a nice idea!
But I think a workaround isn't a real fix (still a fix anyway for my case). I don't know why gotrue lib broke, but I think it should be fix a day
h
this block of code https://github.com/supabase/gotrue-js/blob/797ea09b6188291861b5bed4a488cc74b6f95aaf/src/GoTrueClient.ts#L81-L85 check the url and set the session if it's a redirect from 3rd party login
can you show your login code?
o
Sure, so I'm using Svelte for that, I'm storing the user using a simple store
The ``getAccount`` method just returns the user
But as you saw in the dev tool screenshot, after login using a provider, the session is not saved correctly. It works fine after refresh the page manually
So I investigated the gotrue lib, and tried to debug it manually
h
your getAccount is an api?
can i see that code
o
It just returns ``client.auth.user()``
h
o
So enabling ``detectSessionInUrl``?
h
sorry, wrong url. I fixed already
you set user on the Root of svelte app
I'm not sure about the life cycle of svelte app. But it seems access client.auth.user() from the store doesn't work for your case
o
It works
It works but I need to refresh my page manually after login using a provider
hmm okay, still weird for me anyway, like it shouldn't be a problem to get the current user logged because it is stored in the localstorage, so gotrue is making something weird here. Also, I don't really understand why returning values for the login with provider because we're redirect, so we never get that data (but it's another story)
The main issue here is gotrue which can't get the localStorage correctly
I don't know if it's because of js lang, or if it's a workflow issue
h
no, the issue is getSessionFromUrl is not an await
for it's crucial that your get user runs before or after getSessionFromUrl completes
o
Yeah I got that 😂 but it is a workaround, not a real way to fix that
h
https://supabase.io/docs/guides/with-svelte#launch in the svelte example, it sets the user on startup and also on onAuthStateChange event
o
Yeah, but the issue here is ``client.auth.user()`` should give the current logged user, and not null after login using a provider
h
> not a real way to fix that can you explain more?
> should give the current logged user, and not null after login using a provider it doesn't give you the user because you access it before the user is ready
at the state when you call the method, the user is not ready
o
Simply the way you gave me is technically a good way, but what's ``client.auth.user()`` method if we can't get the user?
But the user is stored in the local storage right?
h
only after the user is pulled from gotrue server then it will persist to local storage
1. you call login with 3rd party login 2. it will redirect back to your website with auth_token. 3. supabase client will get the token and send to Gotrue server for validation then the server will return the user info.
so at step 3. it will take a while for that to happen. That's why when you access it immediately, the user is null. if you refresh again, the user info is already on localstorage. So it can return immediately.
o
Okaaaay make sense to me
Wouldn't be better to open a new window with the provider, and so we can get the user object directly using ``client.auth.signIn({ provider: 'github' })`` for example?
So
Copy code
js
const { user } = await client.auth.signIn({  provider: 'github' })
Would work
Because right now, we can't get the user like that xD
h
> Wouldn't be better to open a new window with the provider, and so we can get the user object directly using client.auth.signIn({ provider: 'github' }) ah not possible. github only return access_token. Then the access_token needs to be verify with supabase gotrue-server which then will return the user if token is valid
o
yeah, or an error if it's not valid right?
h
yah
> Then the access_token needs to be verify with supabase gotrue-server which then will return the user if token is valid anyway, you will need to wait for this validation before you can access the user info
at least that's how supabase gotrue Oauth2 works
hope this help, if not feel free to open an issue on gotrue-js library
o
Nonono you helped me a lot, thanks you for that! I was just curious if we couldn't just open the provider link in a new window instead of a simple redirect, maybe it could be easier?
h
actually it can be improved by doing callback to an api backend route instead of the frontend. The processing token and setting the cookie will be hidden away 😉
however as i know, it's troublesome to use supabase-client on backend api route.
yeah, i may have to admit you are right. It's a workaround
hahaha 😇 hope that we can do better
btw, i love thread in discord. Really help with long discussion like this 👍
o
Yeah that's right! Anyway, thanks you so much for your help and your time!
f
Hi @Olyno please what theme are you using for your vscode? I like it
o
Question not related to the thread, I answer you in #839993398554656831 :p