nicolaslopezj / meteor-apollo-accounts

Meteor accounts in GraphQL
MIT License
146 stars 37 forks source link

Logout is not working #54

Open nimish-gupta opened 7 years ago

nimish-gupta commented 7 years ago

Graphql Error: Cannot read property "userId" of null http://www.awesomescreenshot.com/image/2472459/bb6c0baa15bdd8884caf8abed11d387f

nicolaslopezj commented 7 years ago

Maybe that example is not up to date?

nimish-gupta commented 7 years ago

I have git cloned the version and update all the packages

dbrrt commented 7 years ago

@nimishrocks Since this example, another initialization for GraphQL schema has been implemented which is incompatible with the one in the example (without tweaking a bit). Can you share your GraphQL initialization? And client implementation ?

nicolaslopezj commented 7 years ago

Can anyone confirm this bug?

nikogosovd commented 6 years ago

I have a userId issue on logout. I added Accounts.onLogout hook on the server like this:

Accounts.onLogout(({ user, connection }) => {
  console.log({ user, connection });
  /* other code */
});

And when I call logout on the client I've got user === undefined sometimes.

I think I found the root of the issue. When I modify library code that way (see comments), everything becomes fine:

async function(apollo) {
  const token = await getLoginToken()

  if (!token) return

  await apollo.mutate({ // wait for apollo mutate
    mutation: gql`
      mutation logout($token: String!) {
        logout(token: $token) {
          success
        }
      }
    `,
    variables: {
      token
    }
  })
  await resetStore() // move this line afret apollo.mutate
}

The reason of that behaviour is that Meteor listens for localStorage on the client and when resetStore happens before apollo mutation, Meteor logs out on the client and resolver's context doesn't contain userId anymore when mutation order comes.

Reversing the order prevents this.

@nicolaslopezj Could please you fix the issue with code modification similar to the above?