Skip to content

Commit

Permalink
Fix api-routes-apollo-server-and-client-auth Example (vercel#10334)
Browse files Browse the repository at this point in the history
* 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 apollographql/apollo-cache-persist#34 (comment) comment for more info.

* Fix api-routes-apollo-server-and-client-auth Example (linting)
  • Loading branch information
IvanPaqmind authored and chibicode committed Feb 11, 2020
1 parent c42a0d1 commit 7d21b71
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import Link from 'next/link'
import { withApollo } from '../apollo/client'
import gql from 'graphql-tag'
import { useMutation } from '@apollo/react-hooks'
import { useMutation, useApolloClient } from '@apollo/react-hooks'
import Field from '../components/field'
import { getErrorMessage } from '../lib/form'
import { useRouter } from 'next/router'
Expand All @@ -19,6 +19,7 @@ const SignInMutation = gql`
`

function SignIn() {
const client = useApolloClient()
const [signIn] = useMutation(SignInMutation)
const [errorMsg, setErrorMsg] = React.useState()
const router = useRouter()
Expand All @@ -36,6 +37,7 @@ function SignIn() {
password: passwordElement.value,
},
})
client.resetStore()
if (data.signIn.user) {
router.push('/')
}
Expand Down
@@ -1,6 +1,5 @@
import React from 'react'
import { useMutation } from '@apollo/react-hooks'

import { useMutation, useApolloClient } from '@apollo/react-hooks'
import gql from 'graphql-tag'
import { useRouter } from 'next/router'
import { withApollo } from '../apollo/client'
Expand All @@ -12,16 +11,18 @@ const SignOutMutation = gql`
`

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])
}, [signOut, router, client])

return <p>Signing out...</p>
}
Expand Down

0 comments on commit 7d21b71

Please sign in to comment.