Skip to content

Commit 74c2600

Browse files
authored
Fix api-routes-apollo-server-and-client-auth Example (vercel#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 apollographql/apollo-cache-persist#34 (comment) comment for more info. * Fix api-routes-apollo-server-and-client-auth Example (linting)
1 parent 3cd4d01 commit 74c2600

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

examples/api-routes-apollo-server-and-client-auth/pages/signin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react'
22
import Link from 'next/link'
33
import { withApollo } from '../apollo/client'
44
import gql from 'graphql-tag'
5-
import { useMutation } from '@apollo/react-hooks'
5+
import { useMutation, useApolloClient } from '@apollo/react-hooks'
66
import Field from '../components/field'
77
import { getErrorMessage } from '../lib/form'
88
import { useRouter } from 'next/router'
@@ -19,6 +19,7 @@ const SignInMutation = gql`
1919
`
2020

2121
function SignIn() {
22+
const client = useApolloClient()
2223
const [signIn] = useMutation(SignInMutation)
2324
const [errorMsg, setErrorMsg] = React.useState()
2425
const router = useRouter()
@@ -36,6 +37,7 @@ function SignIn() {
3637
password: passwordElement.value,
3738
},
3839
})
40+
client.resetStore()
3941
if (data.signIn.user) {
4042
router.push('/')
4143
}

examples/api-routes-apollo-server-and-client-auth/pages/signout.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from 'react'
2-
import { useMutation } from '@apollo/react-hooks'
3-
2+
import { useMutation, useApolloClient } from '@apollo/react-hooks'
43
import gql from 'graphql-tag'
54
import { useRouter } from 'next/router'
65
import { withApollo } from '../apollo/client'
@@ -12,16 +11,18 @@ const SignOutMutation = gql`
1211
`
1312

1413
function SignOut() {
14+
const client = useApolloClient()
1515
const router = useRouter()
1616
const [signOut] = useMutation(SignOutMutation)
1717

1818
React.useEffect(() => {
1919
if (typeof window !== 'undefined') {
2020
signOut().then(() => {
21+
client.resetStore()
2122
router.push('/signin')
2223
})
2324
}
24-
}, [signOut, router])
25+
}, [signOut, router, client])
2526

2627
return <p>Signing out...</p>
2728
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy