Created
June 7, 2018 15:41
-
-
Save charyorde/036b372cbb8e15699ec8476af8acefff to your computer and use it in GitHub Desktop.
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
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) | |
} | |
}) | |
} | |
} |
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
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