```ts const onStartup = async () => { if (...
# help
l
Copy code
ts
  const onStartup = async () => {
      if (supabase.auth.session()) {
        if (router.pathname === "/prijava") {
          await router.push("/")
        }
        userState.user_id = supabase.auth.session().user.user_metadata.id
        userState.user_displayName =
          supabase.auth.session().user.user_metadata.name
        userState.user_email = supabase.auth.session().user.user_metadata.email
        userState.user_photoUrl =
          supabase.auth.session().user.user_metadata.avatar_url
        userState.user_isAuth = true
        setLoading(false)
      }

      const auth = supabase.auth.onAuthStateChange(async (event, session) => {
        if (event === "SIGNED_IN") {
          userState.user_id = session.user.user_metadata.id
          userState.user_displayName = session.user.user_metadata.name
          userState.user_email = session.user.user_metadata.email
          userState.user_photoUrl = session.user.user_metadata.avatar_url
          userState.user_isAuth = true
          setLoading(false)
        }
        if (event === "SIGNED_OUT") {
          userState.user_id = null
          userState.user_displayName = null
          userState.user_email = null
          userState.user_photoUrl = null
          userState.user_isAuth = false
          await router.push("/prijava")
          setLoading(false)
        }
      })
      
  }
n
Hello @Luc! 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.
l
I have a problem right here. Auth and supabase.auth.session() takes some time to get everything right. So I cant just check if(!(auth || session)), any solution?
@Olyno
this is because onAuthStateChange doesnt trigger everytime the page refreshes
g
I find this discussion gave me a better understanding of the process and issues involved with client start up timing. https://github.com/supabase/gotrue-js/issues/23 What framework are you using?
n
Luc (2022-05-26)
l
i cant await the session in firebase-js
g
firebase-js? What framework are you using?
l
oh my bad
supabase-js
But like the problem here is that can check if there is a session and add event listener at the same time. If login process is going trough there is no session yet and the code will return that there is no session and skip the listener part. Understand? When I was using FIREBASE it was much easier, but I don't like the way how firebase data query works.
So I switched to Supabase
somehow
g
Yes, I think many agree it is complex and you will find issues and questions about the startup process. Basically if the token is expired and has to be refreshed, or if you are coming in with a signIn/magiclink type url with a token, the handler will trigger with Signin and session. If you have normal localstorage (not React Native or other async storage) then I believe session is available immediately if you have signed before. onAuthStateChange does not get called in the case where session is in local storage, unless token is expired.
l
I just have providers
Copy code
ts
  const onStartup = async () => {
      if (supabase.auth.session()) {
        if (router.pathname === "/prijava") {
          await router.push("/")
        }
        userState.user_id = supabase.auth.session().user.user_metadata.id
        userState.user_displayName =
          supabase.auth.session().user.user_metadata.name
        userState.user_email = supabase.auth.session().user.user_metadata.email
        userState.user_photoUrl =
          supabase.auth.session().user.user_metadata.avatar_url
        userState.user_isAuth = true
        setLoading(false)
      }

      if (!supabase.auth.session()) {
        return setLoading(false)
      }

      ^^^^^^^^^^^

      const auth = supabase.auth.onAuthStateChange(async (event, session) => {
        if (event === "SIGNED_IN") {
          userState.user_id = session.user.user_metadata.id
          userState.user_displayName = session.user.user_metadata.name
          userState.user_email = session.user.user_metadata.email
          userState.user_photoUrl = session.user.user_metadata.avatar_url
          userState.user_isAuth = true
          setLoading(false)
        }
        if (event === "SIGNED_OUT") {
          userState.user_id = null
          userState.user_displayName = null
          userState.user_email = null
          userState.user_photoUrl = null
          userState.user_isAuth = false
          await router.push("/prijava")
          setLoading(false)
        }
      })
      
  }
I use providers to sign in
As you can see\
I have 2 session checks
1 is if the session exists and 1 if not
There is a problem with the one that checks if the session doesnt exist
After sign up session doesnt exist yet
Or maybe after page reload
So it just triggers that statement and returns the children
So it skips the listener
there is my problem
...
@garyaustin
g
You mention signup, I assume if you are using 3rd party login you are doing signIn. When you do the 3rd party signIn (at least the first time) you get a redirect to your page to start running and there will be no session until the onAuthChangeEvent SIGNED_IN occurs.
If you refresh the page after that (while the token is still not expired) you will have session immediately with auth.session() and there will be no auth event.
l
I think you dont understand my question
.
g
That is possible...
l
i guess
like i know that it is but there is a problem
g
So, not sure this addresses your issue but I see you check for session being there with an if and then you do another check to see if it is not there. It is possible (but slim) that it is not ready when you do the first if session() call but is when you do the 2nd !session() if so neither would fire. You probably want an else on the first if. I doubt that is your issue though.
l
Yeah but when I sign in it doesnt trigger the SIGNED_IN event because that else statement returned setLoading
g
When you get redirected back to your app from the 3rd party signIn process, the onAuthChangeEvent does not get fired?
l
nope lol
because, I said earlier. There is no session for a sec after sign in, and code just triggers the else statement that gets out of the func
g
Yeah, I see the return now. You need to make sure your the handler gets set up before your return.
l
that is my main problem yeah lol
any suggestions?
g
Why can't you just set it up first?
l
wdym by that? to just like move the handler at the top of the func/
?
g
Yes
l
now its just worse.. The css styles dont apply just in the moment. And SIGNED_IN doesnt trigger
can i suggest that yall make the this
onAuthChangedState
run on every call like even if there is no events like firebase does?
this shi sux tbh
g
There are issues around for quite awhile suggesting that. You can add to them, or start a new issue on github. I'm not a SB employee, so Y'all does not apply.
l
okay thanks.
it would make supabase 10 times better tbh