Created
April 12, 2020 20:08
-
-
Save BadPirate/cdc37d60e30d0ef9476fb3da5fe5b5e7 to your computer and use it in GitHub Desktop.
Hasura Firebase Token Refresh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
firebase.auth().onAuthStateChanged((firebaseUser) => { | |
if (firebaseUser) { | |
return firebaseUser.getIdToken().then((token) => firebase.auth().currentUser.getIdTokenResult() | |
.then((result) => { | |
if (result.claims['https://hasura.io/jwt/claims']) { | |
return token | |
} | |
const endpoint = 'https://xxx.cloudfunctions.net/refreshToken' | |
return fetch(`${endpoint}?uid=${firebaseUser.uid}`).then((res) => { | |
if (res.status === 200) { | |
return firebaseUser.getIdToken(true) | |
} | |
return res.json().then((e) => { throw e }) | |
}) | |
})).then((validToken) => { | |
// Store Token / Or create Apollo with your new token! | |
}).catch(console.error) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment