Anyone want to take a stab at my Stack Overflow qu...
# help
v
Anyone want to take a stab at my Stack Overflow question regarding OnAuthStateChanged? https://stackoverflow.com/questions/72385641/supabase-onauthstatechanged-how-do-i-properly-wait-for-the-request-to-finish-p
n
Hello @Vik! This thread has been automatically created from your message in #843999948717555735 a few seconds ago. We have already mentioned the @User so that they can see your message and help you as soon as possible! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ``...`` menu) and select "Leave Thread" to unsubscribe from future updates. Want to change the title? Use the ``/title`` command! We have solved your problem? Click the button below to archive it.
g
Just a quick glance. You use await on sb.auth.session() but that returns the current client variable immediately whether set or not. It takes some small amount of time for the SB client to check local storage, or more if token needs refreshing.
Copy code
/**
   * Returns the session data, if there is an active session.
   */
  session(): Session | null {
    return this.currentSession
  }
n
Vik (2022-05-26)
j
I replied in stack overflow, and it's exactly what gary said is your problem.
v
Hey, I went ahead and updated my code to the current version like you asked.
j
@Vik set the listener before the session call
@Vik any luck?
v
Hey, sorry away from my computer. I’ll test shortly and let you know ❤️
Is this what you mean?
Copy code
const { data: listener } = supabase.auth.onAuthStateChange((_: any, session: any) => {
      setUser(session?.user ?? null);
    });

    const session = supabase.auth.session();
    setUser(session?.user ?? null);
j
Yup
v
Still getting that flashing 😦
j
Do you know how to debug in the browser?
v
Yeah I do
j
I would add a debugger statement and step through the variables
See what happens with each step
v
Ok I’ll test that out, thank you
j
No worries
I would not expect that it will follow the steps I listed in the stack over flow now that you’ve fixed it up
Hey Vik, I found something that I think might be helpful for you...
v
Hey @jaitaiwan ! Oh man I’ve been seeing a lot more articles pop up about not using useeffect lol
Let me read this now
j
Yeah do it... because part of this I think related to your auth state issue. Also if you still have that auth issue, there's a refreshSession() function that you can use.
@Vik to solve the issue I had to detect the hash variables and show loading
Where
redirecting()
is a component that shows a loader. That's inside my function I have