-
-
Save kdepp/8550f017b8cc5f0abdc74ecb90ce6a98 to your computer and use it in GitHub Desktop.
withAccessToken
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
const getAccessToken = () => { | |
const pGetToken = db ? getAccessTokenFromDB() : Promise.resolve() | |
return pGetToken.then(token => { | |
if (token) return token | |
return getAccessTokenFromRemote() | |
.then(data => { | |
if (!data || !data.access_token) { | |
throw new Error('No access token in token api response') | |
} | |
if (!db) return data.access_token | |
return db.ref('paypal_access_token') | |
.set({ | |
access_token: data.access_token, | |
expire: new Date() * 1 + data.expires_in * 1000 | |
}) | |
.then(() => data.access_token) | |
}) | |
}) | |
.then(token => { | |
console.log('Got paypal access token', token) | |
jwtRequest.saveToken(token) | |
}) | |
.catch(e => { | |
console.error(e.stack) | |
}) | |
} |
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
const withAccessToken = (fn) => (...args) => { | |
const accessToken = jwtRequest.getToken() | |
const p = accessToken ? Promise.resolve() | |
: getAccessTokenFromRemote().then(data => { | |
console.log('remote access token', data.access_token) | |
jwtRequest.saveToken(data.access_token) | |
}) | |
return p.then(() => fn(...args)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment