Unverified Commit 74c2600b authored by Ivan Kleshnin's avatar Ivan Kleshnin Committed by GitHub

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)
parent 3cd4d01d
...@@ -2,7 +2,7 @@ import React from 'react' ...@@ -2,7 +2,7 @@ import React from 'react'
import Link from 'next/link' import Link from 'next/link'
import { withApollo } from '../apollo/client' import { withApollo } from '../apollo/client'
import gql from 'graphql-tag' import gql from 'graphql-tag'
import { useMutation } from '@apollo/react-hooks' import { useMutation, useApolloClient } from '@apollo/react-hooks'
import Field from '../components/field' import Field from '../components/field'
import { getErrorMessage } from '../lib/form' import { getErrorMessage } from '../lib/form'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
...@@ -19,6 +19,7 @@ const SignInMutation = gql` ...@@ -19,6 +19,7 @@ const SignInMutation = gql`
` `
function SignIn() { function SignIn() {
const client = useApolloClient()
const [signIn] = useMutation(SignInMutation) const [signIn] = useMutation(SignInMutation)
const [errorMsg, setErrorMsg] = React.useState() const [errorMsg, setErrorMsg] = React.useState()
const router = useRouter() const router = useRouter()
...@@ -36,6 +37,7 @@ function SignIn() { ...@@ -36,6 +37,7 @@ function SignIn() {
password: passwordElement.value, password: passwordElement.value,
}, },
}) })
client.resetStore()
if (data.signIn.user) { if (data.signIn.user) {
router.push('/') router.push('/')
} }
......
import React from 'react' import React from 'react'
import { useMutation } from '@apollo/react-hooks' import { useMutation, useApolloClient } from '@apollo/react-hooks'
import gql from 'graphql-tag' import gql from 'graphql-tag'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import { withApollo } from '../apollo/client' import { withApollo } from '../apollo/client'
...@@ -12,16 +11,18 @@ const SignOutMutation = gql` ...@@ -12,16 +11,18 @@ const SignOutMutation = gql`
` `
function SignOut() { function SignOut() {
const client = useApolloClient()
const router = useRouter() const router = useRouter()
const [signOut] = useMutation(SignOutMutation) const [signOut] = useMutation(SignOutMutation)
React.useEffect(() => { React.useEffect(() => {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
signOut().then(() => { signOut().then(() => {
client.resetStore()
router.push('/signin') router.push('/signin')
}) })
} }
}, [signOut, router]) }, [signOut, router, client])
return <p>Signing out...</p> return <p>Signing out...</p>
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment