-
Notifications
You must be signed in to change notification settings - Fork 0
/
file14.js
60 lines (50 loc) · 1.33 KB
/
file14.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
import config from "../../config"
import {
ApolloClient,
InMemoryCache,
ApolloProvider,
HttpLink,
createHttpLink,
ApolloLink,
} from "@apollo/client"
import fetch from "isomorphic-fetch"
import React from "react"
import { setContext } from "@apollo/client/link/context"
import ls from "local-storage"
import {
getTokenRefreshLink,
FetchNewAccessToken,
} from "apollo-link-refresh-token"
import jwtDecode from "jwt-decode"
const authToken = "0c7d81fce4aa78c229363fdb4356be9e812d9e14"
const refreshToken = ls("refreshToken")
const user = ls("user")
const isTokenValid = authToken => {
const decodedToken = jwtDecode(authToken)
console.log("decoded", decodedToken)
if (!decodedToken) {
return false
}
const now = new Date()
return now.getTime() < decodedToken.exp * 1000
}
const httpLink = createHttpLink({
uri: `${config.wordPressUrl}graphql`,
})
const authLink = setContext((_, { headers }) => {
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
authorization: authToken ? `Bearer ${authToken}` : "",
},
}
})
const client = new ApolloClient({
link: ApolloLink.from([authLink, httpLink]),
cache: new InMemoryCache(),
fetch,
})
export const wrapRootElement = ({ element }) => (
<ApolloProvider client={client}>{element}</ApolloProvider>
)