BOD-2 More cleanup after fixing JWT tokens.
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { onError } from "apollo-link-error";
|
||||
import { auth } from "../firebase/firebase.utils";
|
||||
//https://stackoverflow.com/questions/57163454/refreshing-a-token-with-apollo-client-firebase-auth
|
||||
|
||||
const errorLink = onError(
|
||||
@@ -11,40 +10,6 @@ const errorLink = onError(
|
||||
)
|
||||
);
|
||||
if (networkError) console.log(`[Network error]: ${networkError}`);
|
||||
|
||||
let expired = false;
|
||||
if (graphQLErrors) {
|
||||
if (graphQLErrors[0].message.includes("JWTExpired")) {
|
||||
expired = true;
|
||||
}
|
||||
}
|
||||
if (networkError) {
|
||||
if (networkError.message.includes("JWTExpired")) {
|
||||
expired = true;
|
||||
}
|
||||
}
|
||||
if (expired) {
|
||||
//User access token has expired
|
||||
console.log("Old Token", window.localStorage.getItem("token"));
|
||||
// Let's refresh token through async request
|
||||
|
||||
auth.currentUser.getIdToken(true).then(token => {
|
||||
if (token) {
|
||||
console.log("Got the new token.", token);
|
||||
window.localStorage.setItem("token", token);
|
||||
|
||||
const oldHeaders = operation.getContext().headers;
|
||||
operation.setContext({
|
||||
headers: {
|
||||
...oldHeaders,
|
||||
authorization: token ? `Bearer ${token}` : ""
|
||||
}
|
||||
});
|
||||
console.log(operation.getContext());
|
||||
return forward(operation);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user