-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
63 lines (56 loc) · 2.01 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { StatusBar } from "expo-status-bar";
import { GestureHandlerRootView } from "react-native-gesture-handler";
import {
ApolloClient,
InMemoryCache,
ApolloLink,
createHttpLink,
ApolloProvider,
} from "@apollo/client";
import { setContext } from "@apollo/client/link/context";
import NavigationFlows from "./src/navigation/NavigationFlows";
import { Provider } from "react-redux";
import { store } from "./src/redux/store";
import { loadErrorMessages, loadDevMessages } from "@apollo/client/dev";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { RootSiblingParent } from "react-native-root-siblings";
// Create an HTTP link for the GraphQL endpoint
const httpLink = createHttpLink({
uri: "https://stockapp-backend-4u6j.onrender.com/graphql/", // Replace with your GraphQL endpoint
});
// Middleware to set JWT token in the headers
const authLink = setContext(async (_, { headers }) => {
// Get the JWT token from wherever you store it (localStorage, AsyncStorage, etc.)
// const jwtToken = localStorage.getItem('jwtToken'); // Example: Using localStorage
const jwtToken = await AsyncStorage.getItem("token"); // Example: Using localStorage
// Return the headers object with the JWT token included
return {
headers: {
...headers,
authorization: jwtToken ? `JWT ${jwtToken}` : "", // Include the token in the authorization header
},
};
});
export const client = new ApolloClient({
link: ApolloLink.from([authLink, httpLink]),
cache: new InMemoryCache(),
});
export default function App() {
return (
<ApolloProvider client={client}>
<Provider store={store}>
<GestureHandlerRootView style={{ flex: 1 }}>
<StatusBar style="auto" translucent barStyle="light-content" />
<RootSiblingParent>
<NavigationFlows />
</RootSiblingParent>
</GestureHandlerRootView>
</Provider>
</ApolloProvider>
);
}
if (__DEV__) {
// Adds messages only in a dev environment
loadDevMessages();
loadErrorMessages();
}