Skip to content

Instantly share code, notes, and snippets.

@charyorde
Created June 7, 2018 15:41
Show Gist options
  • Save charyorde/036b372cbb8e15699ec8476af8acefff to your computer and use it in GitHub Desktop.
Save charyorde/036b372cbb8e15699ec8476af8acefff to your computer and use it in GitHub Desktop.
export function checkCloudToken(token) {
return function(dispatch) {
return self.fetch('/api/token/validate', {
method: 'POST',
credentials: 'include',
body: JSON.stringify({ token })
})
.then((resp) => {
if (resp.status == 200) {
// do nothing
return
}
else {
// destroy session
dispatch(logout())
setTimeout(() => {
history.replace('/login')
}, 1000)
}
})
}
}
import React from 'react';
import asyncPoll from 'react-async-poll';
import * as actions from '../../actions/user';
class KeepCloudAlive extends React.Component {
render() {
return(
<div></div>
)
}
}
const onPollInterval = (props, dispatch) => {
/*
In this example, dispatch will return a Promise
and then call this function again [intervalDuration]
milliseconds later once the Promise has resolved
*/
if (props.user) {
dispatch(actions.checkCloudToken(props.user.token));
}
};
/*
The first invocation of asyncPoll will return a function
that accepts only one argument: your component
*/
export default asyncPoll(60 * 1000, onPollInterval)(KeepCloudAlive);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment