auth0-blog/gatsby-auth0

setSession errors when refreshing the page after login (no persistent user data)

Opened this issue · 2 comments

Hi,

I am in the process of trying to create some clientside routes in a gatsby project I am working on. I discovered your blog post on the Auth0 website and followed it to the letter. However, I was unsuccessful in getting it to work for the error I have mentioned in the title. I then proceeded to clone the repo and see if this project would work in the hope that it was just due to an updated dependency causing the problem. However, again the same problem.

I have looked over the code numerous times, even forked and merged the PR you made some time ago to see if that was the fix, but sadly that too was unsuccessful.

I console logged the error and it just says the user isn't logged in. My guess is that there's more to it than that, but I'm new to Auth0 so not sure where I should look.

Do you have any idea what might be causing the issue?

const setSession = (cb = () => {}) => (err, authResult) => {
  if (err) { // for context, this is where the issue is - this error exists and therefore the user is redirected to the homepage despite still having a valid session (as the user can again navigate to /account and it still shows hello friend)
    navigate("/")
    cb()
    return
  }

  if (authResult && authResult.accessToken && authResult.idToken) {
    let expiresAt = authResult.expiresIn * 1000 + new Date().getTime()
    tokens.accessToken = authResult.accessToken
    tokens.idToken = authResult.idToken
    tokens.expiresAt = expiresAt
    user = authResult.idTokenPayload
    localStorage.setItem("isLoggedIn", true)
    const redirect = authResult.appState || "/"
    navigate(redirect)
    cb()
  }
}

Facing same issue, still looking for the solution