Unverified Commit d04d9766 authored by Ivan Kleshnin's avatar Ivan Kleshnin Committed by GitHub

Improve api-routes-apollo-server-and-client-auth Example (#10358)

1. Prevent possible race conditions of cache pruning, refetching and redirects

2. Note: the original code has the following defect.

SignOut action restarts the dev server effectively resetting the memory and erasing all the "registered users" data. You have to SignUp again after SignOut. I'm not sure how
to properly fix it a.t.m
parent 9bbf31ca
...@@ -31,15 +31,15 @@ function SignIn() { ...@@ -31,15 +31,15 @@ function SignIn() {
const passwordElement = event.currentTarget.elements.password const passwordElement = event.currentTarget.elements.password
try { try {
await client.resetStore()
const { data } = await signIn({ const { data } = await signIn({
variables: { variables: {
email: emailElement.value, email: emailElement.value,
password: passwordElement.value, password: passwordElement.value,
}, },
}) })
client.resetStore()
if (data.signIn.user) { if (data.signIn.user) {
router.push('/') await router.push('/')
} }
} catch (error) { } catch (error) {
setErrorMsg(getErrorMessage(error)) setErrorMsg(getErrorMessage(error))
......
...@@ -16,12 +16,11 @@ function SignOut() { ...@@ -16,12 +16,11 @@ function SignOut() {
const [signOut] = useMutation(SignOutMutation) const [signOut] = useMutation(SignOutMutation)
React.useEffect(() => { React.useEffect(() => {
if (typeof window !== 'undefined') { signOut().then(() => {
signOut().then(() => { client.resetStore().then(() => {
client.resetStore()
router.push('/signin') router.push('/signin')
}) })
} })
}, [signOut, router, client]) }, [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