Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
.then(creds => {
if (creds) {
const data = JSON.parse(creds.password);
const nowSeconds = (new Date()).getTime() / 1000;
const expiration = new Date(data.accessTokenExpirationDate).getTime() / 1000;
if (data.refreshToken && expiration < nowSeconds) {
return refresh(config, { refreshToken: data.refreshToken })
.then(
saveAuthResponse,
() => {
// Null token will produce an error where it is used.
return null;
});
}
return data.accessToken;
}
return null;
});
}
.then((resp) => {
// if 200 then token is valid, no need to review
if (resp.status !== 200) {
// if error attempt to renew access token
return refresh(refreshConfig, {
refreshToken: refreshToken
});
}
}, () => {
throw new Error('Failed to verify token');
refresh = async () => {
try {
const authState = await refresh(config, {
refreshToken: this.state.refreshToken
});
this.animateState({
accessToken: authState.accessToken || this.state.accessToken,
accessTokenExpirationDate:
authState.accessTokenExpirationDate || this.state.accessTokenExpirationDate,
refreshToken: authState.refreshToken || this.state.refreshToken
});
} catch (error) {
Alert.alert('Failed to refresh token', error.message);
}
};
refresh = async () => {
try {
const authState = await refresh(config, {
refreshToken: this.state.refreshToken,
});
this.animateState({
accessToken: authState.accessToken || this.state.accessToken,
accessTokenExpirationDate:
authState.accessTokenExpirationDate || this.state.accessTokenExpirationDate,
refreshToken: authState.refreshToken || this.state.refreshToken,
});
} catch (error) {
Alert.alert('Failed to refresh token', error.message);
}
};
refresh = async () => {
try {
const authState = await refresh(config, {
refreshToken: this.state.refreshToken
});
this.animateState({
accessToken: authState.accessToken || this.state.accessToken,
accessTokenExpirationDate:
authState.accessTokenExpirationDate || this.state.accessTokenExpirationDate,
refreshToken: authState.refreshToken || this.state.refreshToken
});
} catch (error) {
Alert.alert('Failed to refresh token', error.message);
}
};