Last active
May 30, 2020 16:41
-
-
Save SaraChicaD/4aa5d355756e27acc3e3ef532d8ed60d to your computer and use it in GitHub Desktop.
This file contains 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
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