Skip to content

Instantly share code, notes, and snippets.

@JustinDFuller
Created August 12, 2018 01:48
Show Gist options
  • Select an option

  • Save JustinDFuller/1bdcf88233f2b7e1966775ebaa2497ec to your computer and use it in GitHub Desktop.

Select an option

Save JustinDFuller/1bdcf88233f2b7e1966775ebaa2497ec to your computer and use it in GitHub Desktop.
Using promise-funnel for authentication
import createFunnel from 'promise-funnel'
const funnel = createFunnel()
function fetchData(...options) {
// This will fail if the user is unauthenticated
return fetch(...options)
}
function login() {
// all requests will be stopped while authentication is happening
funnel.cork()
// do login logic here
// All the requests will happen now that the user is authenticated again
funnel.uncork()
}
setTimeout(login, 60 * 5 * 1000 /* re-login every five minutes */)
export default {
fetchData: funnel.wrap(fetchData),
login,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment