Getting the user back from a call to `logIn`

I’m working on trying to get the new auth module integrated into an app, using netlify auth, and have run into an issue where I’m not sure if this is a bug, or if I’m doing something wrong. From what I can tell from looking at the code, it appears as if the code is supposed to be returning the user object when the call to logIn succeeds. I’m trying to get that user from the logIn call, so I can set it in application state, but I’m not actually getting anything from the call. My code looks something like this:

const { logIn } = useAuth()

// ...

const login = async () => {
  const user = await logIn()
  dispatch("loggedIn", user) // <-- `user` here is always null
}

Am I misunderstanding that the call to logIn should be returning the user? I’ve also tried this as well:

const { currentUser, logIn } = useAuth()

// ...

const login = async () => {
  await logIn()
  dispatch("loggedIn", currentUser) // <-- `currentUser` here is always null as well
}

I feel like in this case it’s just a timing issue, because I can bind a UI element to currentUser.email and it gets rendered just fine.

What’s the right way to do what I’m trying here?

The first case does appear like a bug, I’ll take a look at the netlify implementation to figure out what’s going on, you could try something like this:

const { logIn, getUserMetadata } = useAuth()

// ...

const login = async () => {
  const user = await logIn()
  const userMetadata = await getUserMetadata()
  dispatch("loggedIn", userMetadata)
}

I’ll give that a shot, thanks for the fast reply!

Oh, I almost forgot…as long as you’re taking a look at it, if I recall correctly, I also tried this with no luck as well:

const { logIn, getCurrentUser } = useAuth()

// ...

const login = async () => {
  await logIn()
  const user = await getCurrentUser()
  dispatch("loggedIn", user) // <-- `user` was null here
}

Am I misunderstanding how getCurrentUser is supposed to work? Based on the provider code, I was expecting that to return the current user.

@ryexley Because of the way that hooks work, I think you’ll have to useEffect, something along the lines of:


const { isAuthenticated, currentUser, logIn } = useAuth()


useEffect(() => {
    if (isAuthenticated) {
        dispatch("loggedIn", currentUser)
    }
}, [isAuthenticated, currentUser])