Created
August 13, 2020 16:41
-
-
Save elsetiyawan/82399beef7f93a8a0817057e0facaa8b 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
export async function refreshToken() { | |
const token = getCookie("ACCESS_TOKEN"); | |
const refresh_token = getCookie("REFRESH_TOKEN"); | |
const SERVER_DOMAIN = process.env.REACT_APP_API_ENDPOINT; | |
const current_time = new Date(); | |
if (token !== "") { | |
const decodeToken = jwt(token); | |
if (current_time.getTime() > decodeToken.exp * 1000) { | |
return Axios.post(`${SERVER_DOMAIN}v1/auth/refresh`, { | |
refreshToken: refresh_token, | |
}).then((response) => { | |
setCookie("ACCESS_TOKEN", response.data.accessToken, 1); | |
return response.data.accessToken; | |
}); | |
} else { | |
return token; | |
} | |
} | |
return token; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment