From d3aa32edb9c9be1483728a402cc85d64d660b7cf Mon Sep 17 00:00:00 2001 From: Ivan Kleshnin Date: Fri, 31 Jan 2020 16:27:20 +0200 Subject: [PATCH] Improve api-routes-apollo-server-and-client-auth Example 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 --- .../pages/signin.js | 4 ++-- .../pages/signout.js | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/examples/api-routes-apollo-server-and-client-auth/pages/signin.js b/examples/api-routes-apollo-server-and-client-auth/pages/signin.js index d5a2acfb2ab7fab..766a4bd2ca41542 100644 --- a/examples/api-routes-apollo-server-and-client-auth/pages/signin.js +++ b/examples/api-routes-apollo-server-and-client-auth/pages/signin.js @@ -31,15 +31,15 @@ function SignIn() { const passwordElement = event.currentTarget.elements.password try { + await client.resetStore() const { data } = await signIn({ variables: { email: emailElement.value, password: passwordElement.value, }, }) - client.resetStore() if (data.signIn.user) { - router.push('/') + await router.push('/') } } catch (error) { setErrorMsg(getErrorMessage(error)) diff --git a/examples/api-routes-apollo-server-and-client-auth/pages/signout.js b/examples/api-routes-apollo-server-and-client-auth/pages/signout.js index 2580af34888537a..0d18c898278162b 100644 --- a/examples/api-routes-apollo-server-and-client-auth/pages/signout.js +++ b/examples/api-routes-apollo-server-and-client-auth/pages/signout.js @@ -16,12 +16,11 @@ function SignOut() { const [signOut] = useMutation(SignOutMutation) React.useEffect(() => { - if (typeof window !== 'undefined') { - signOut().then(() => { - client.resetStore() + signOut().then(() => { + client.resetStore().then(() => { router.push('/signin') }) - } + }) }, [signOut, router, client]) return

Signing out...