Skip to content

Instantly share code, notes, and snippets.

@SaraChicaD
Last active May 30, 2020 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SaraChicaD/4aa5d355756e27acc3e3ef532d8ed60d to your computer and use it in GitHub Desktop.
Save SaraChicaD/4aa5d355756e27acc3e3ef532d8ed60d to your computer and use it in GitHub Desktop.
setData + getData are async storage functions
// thunk
export const fetchRefreshToken = () => (dispatch, getState) => {
const token = `${getState().auth.refreshToken}`;
const params = { "token": token };
dispatch(refreshTokenRequest());
return axios.get(`${API_URL}v1/refreshToken`, {params})
.then((res => {
// 3.75 hrs = 13500
// 4 hrs token expires = 14400
const now = (Date.now() + (13500*1000)).toString();
dispatch(refreshTokenSuccess(res));
setData('tokenExpires', now);
setData('refreshToken', res);
}), err => {
dispatch(refreshTokenError(err));
});
};
// first screen in app nav
componentDidMount() {
getData('tokenExpires')
.then(tokenExpires => {
const now = Date.now();
const expires = parseInt(tokenExpires);
if (expires < now) {
this.props.fetchRefreshToken();
}
});
}
onDidFocus = () => {
getData('tokenExpires')
.then(tokenExpires => {
const now = Date.now();
const expires = parseInt(tokenExpires);
if (expires < now) {
this.props.fetchRefreshToken();
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment