ryanmagoon
05/11/2018, 1:09 AMresetStore()
before my auth token has a chance to update on login. I imagine this is a common problem
if I do a setTimeout
wrapping around resetStore
after the login for a second or so I get my app to kick me to the home screen after login, but without it I just stay on the login screen and I only get to the home screen after a refresh when it then picks up the token
(I’m on react native)
is there a common pattern for dealing with this?max
05/11/2018, 7:43 AMresetStore()
during login?max
05/11/2018, 7:44 AMclient.cache.reset()
to reset the cache.ryanmagoon
05/11/2018, 5:33 PMsetContext
with the auth stuff in the request
param in apolloryanmagoon
05/11/2018, 5:33 PMconst client = new ApolloClient({
request: async operation => {
const token = await getToken()
operation.setContext({
headers: {
authorization: token ? `Bearer ${token}` : ''
}
})
},
uri: '<http://localhost:4000>'
})
ryanmagoon
05/11/2018, 5:34 PMryanmagoon
05/11/2018, 5:34 PMconst NavWrapper = ({ loading, me }) => {
if (loading) {
return (
<SafeAreaView style={{ flex: 1, justifyContent: 'center' }}>
<ActivityIndicator size="large" />
</SafeAreaView>
)
}
return me ? <AppStack /> : <AuthStack />
}
const userQuery = gql`
query userQuery {
me {
id
email
name
}
}
`
export default graphql(userQuery, {
props: ({ data }) => ({ ...data })
})(NavWrapper)
ryanmagoon
05/11/2018, 5:35 PMmax
05/11/2018, 6:58 PM//use the Token in localStorage for authorized requests
const middlewareLink = setContext(() => ({
headers: { authorization: localStorage.getItem('token') ? `Bearer ${localStorage.getItem('token')}` : null }
}));
// const httpLink = createHttpLink({ uri: `http://${process.env.API_URL}` });
const httpLink = createHttpLink({
uri: process.env.API_URL,
batchInterval: 100
});
const link = middlewareLink.concat(httpLink);
const client = new ApolloClient({ link: link, cache: new InMemoryCache() });
max
05/11/2018, 6:59 PM