-
Notifications
You must be signed in to change notification settings - Fork 5
/
refreshAuthToken.js
63 lines (58 loc) · 2.18 KB
/
refreshAuthToken.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import Helper from "../Utility/helper";
import config from "../../config.json";
/**
* Functional component used to retrieve a new access token when access token has become invalid
* @param props.accessTokenHandler Handler function from RouteComponents that updates status of access token
*/
export default function RefreshAccessToken(props) {
// Uses Helper apiCall function to call Sonos API
const helper = new Helper();
// Gets access token's refresh token from local storage
let refreshToken = JSON.parse(
window.localStorage.accessToken
).refresh_token;
let endPoint = config.apiEndPoints.createRefreshAuthTokenURL;
// Calls Sonos API to request fresh access token
const HEADER_BASIC = helper.getHeadersBasic();
const data = {
grant_type: "refresh_token",
refresh_token: refreshToken,
};
const dataKeyValue = Object.keys(data)
.map((key, index) => `${key}=${encodeURIComponent(data[key])}`)
.join("&");
helper.apiCall(endPoint, HEADER_BASIC, "POST", dataKeyValue)
.then((refreshTokenResponse) => {
// If access token is successfully retrieved, update the value of the currently stored token and
// notify RouteComponents that the access token is valid
if (refreshTokenResponse !== undefined) {
updateAccessToken(refreshTokenResponse.data);
props.accessTokenHandler("VALID");
}
})
.catch(function (error) {
// If access token is not successfully retrieved, notify RouteComponents that the user needs to log in
console.error(error);
props.accessTokenHandler("DOES NOT EXIST");
});
};
/**
* Sets the stored value of the Sonos API access token
* @param response Sonos API response when requesting a refreshed access token
*/
function updateAccessToken(response) {
if (response) {
const accessTokenData = {
token: response["access_token"],
refresh_token: response["refresh_token"],
token_type: response["token_type"],
expiry: response["expires_in"],
tokenTimestamp: Math.floor(Date.now() / 1000),
};
// Can be accessed from other pages within the sample app
window.localStorage.setItem(
"accessToken",
JSON.stringify(accessTokenData)
);
}
}