i created a context to start up the socket
but before that it validate the token sending a request to the server.
there is an issue that i can’t really figure it out.
if the app is closed and i open it i receive this error
ERROR Error validating token: [TypeError: Cannot read property 'getString' of undefined
if i keep the app open, and i Reload the app i see all the logs and data correctly.
i tried setting up a timeout of 1minute since the app opens and i have no errors.
but this approach is not ok.
so i thought the context loaded before the client actually was actually setted up ? no idea
useEffect(() => {
const validateToken = async () => {
try {
const { data } = await client.query({
query: VALIDATE_TOKEN,
fetchPolicy: 'network-only',
});
console.log('HEREHEREHERE');
console.log(data);
if (data && data.ValidateToken) {
setSenderID(data.ValidateToken._id);
setSenderUserName(data.ValidateToken.userName);
setIsTokenValid(true);
} else {
console.error('Token validation failed');
setIsTokenValid(false);
}
} catch (error) {
console.error('Error validating token:', error);
setIsTokenValid(false);
}
};
validateToken();
}, []);
import { ApolloClient, HttpLink, ApolloLink } from '@apollo/client';
import { InMemoryCache } from '@apollo/client/cache';
import { storage } from './storage'
import { SERVER } from '@env';
const httpLink = new HttpLink({ uri: `http://${SERVER}:5552/graphql` });
const authLink = new ApolloLink((operation, forward) => {
const token = storage.getString('UID');
operation.setContext({
headers: {
Authorization: token ? `Bearer ${token}` : '',
}
});
return forward(operation);
});
const client = new ApolloClient({
//uri: 'http://10.0.2.2:5552/graphql',
link: authLink.concat(httpLink),
cache: new InMemoryCache({}),
});
export default client
const Root = () => {
return (
<ApolloProvider client={client}>
<WebSocketProvider>
<App />
</WebSocketProvider>
</ApolloProvider>
);
};