rsnext/examples/api-routes-apollo-server-and-client-auth/pages/signout.js
Ivan Kleshnin 74c2600b46
Fix api-routes-apollo-server-and-client-auth Example (#10334)
* Fix api-routes-apollo-server-and-client-auth Example

`client.resetStore()` must be called after SignIn, SignOut actions

Otherwise, even the current basic auth is not working 100% of the time...

For example, as caching occurs here:

```
const { data, loading } = useQuery(ViewerQuery)
```

it sometimes (race conditions!) prevents a user from signing in
the current code base.

Check https://github.com/apollographql/apollo-cache-persist/issues/34#issuecomment-371177206 comment for more info.

* Fix api-routes-apollo-server-and-client-auth Example (linting)
2020-01-30 14:33:42 +01:00

30 lines
694 B
JavaScript

import React from 'react'
import { useMutation, useApolloClient } from '@apollo/react-hooks'
import gql from 'graphql-tag'
import { useRouter } from 'next/router'
import { withApollo } from '../apollo/client'
const SignOutMutation = gql`
mutation SignOutMutation {
signOut
}
`
function SignOut() {
const client = useApolloClient()
const router = useRouter()
const [signOut] = useMutation(SignOutMutation)
React.useEffect(() => {
if (typeof window !== 'undefined') {
signOut().then(() => {
client.resetStore()
router.push('/signin')
})
}
}, [signOut, router, client])
return <p>Signing out...</p>
}
export default withApollo(SignOut)