-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
101 lines (90 loc) · 2.33 KB
/
App.tsx
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import React from "react";
import AsyncStorage from "@react-native-community/async-storage";
import { ApolloClient } from "apollo-client";
import { ApolloProvider } from "@apollo/react-hooks";
import { setContext } from "apollo-link-context";
import { InMemoryCache } from "apollo-cache-inmemory";
import { onError } from "apollo-link-error";
import { HttpLink } from "apollo-link-http";
import unfetch from "unfetch";
import RootStackScreen from "./src/nagivation";
import { StateProvider } from "./src/store";
import { enableScreens } from "react-native-screens";
import * as TaskManager from 'expo-task-manager'
import { useFonts } from 'expo-font';
enableScreens();
const cache = new InMemoryCache();
const httpLink = new HttpLink({
uri: "http://localhost:4000/graphql",
fetch: unfetch,
});
const getAuthToken = async () => {
try {
const value = await AsyncStorage.getItem("AUTH_TOKEN");
if (value !== null) {
return value;
}
} catch (error) {
return error;
}
};
// middleware for requests
const authLink = setContext(async (req, previousContext) => {
// get the authentication token from local storage if it exists
const jwt = await getAuthToken();
if (jwt) {
return {
headers: {
authorization: `Bearer ${jwt}`,
},
};
}
return previousContext;
});
const errorLink = onError(async ({ graphQLErrors, networkError }) => {
let shouldLogout = false;
if (graphQLErrors) {
graphQLErrors.forEach(({ message, locations, path }) => {
if (message === "Unauthorized") {
shouldLogout = true;
}
});
if (shouldLogout) {
try {
await AsyncStorage.setItem("AUTH_TOKEN", "");
} catch (e) {
return e;
}
}
}
if (networkError) {
if (networkError.statusCode === 401) {
try {
await AsyncStorage.setItem("AUTH_TOKEN", "");
} catch (e) {
return e;
}
}
}
});
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache,
errorLink,
});
const App = () => {
const [loaded] = useFonts({
Lobster: require('./assets/fonts/Lobster-Regular.ttf'),
});
if (!loaded) {
return null;
}
return (
<ApolloProvider client={client}>
<StateProvider>
<RootStackScreen getAuthToken={getAuthToken} />
</StateProvider>
</ApolloProvider>
);
};
export default App;