BOD-2 More cleanup after fixing JWT tokens.

This commit is contained in:
Patrick Fic
2020-03-24 18:59:56 -07:00
parent 1c4a5d5b63
commit 1409ef058c
11 changed files with 38 additions and 359 deletions

View File

@@ -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);
}
});
}
}
);