Token Util
1
import axios from 'axios';
2
import Cookie from 'universal-cookie';
3
​
4
export async function getTokenOrRefresh() {
5
const cookie = new Cookie();
6
const speechToken = cookie.get('speech-token');
7
​
8
if (speechToken === undefined) {
9
try {
10
const res = await axios.get('/api/get-speech-token');
11
const token = res.data.token;
12
const region = res.data.region;
13
cookie.set('speech-token', region + ':' + token, {maxAge: 540, path: '/'});
14
​
15
// console.log('Token fetched from back-end: ' + token);
16
return { authToken: token, region: region };
17
} catch (err) {
18
console.log(err.response.data);
19
return { authToken: null, error: err.response.data };
20
}
21
} else {
22
// console.log('Token fetched from cookie: ' + speechToken);
23
const idx = speechToken.indexOf(':');
24
return { authToken: speechToken.slice(idx + 1), region: speechToken.slice(0, idx) };
25
}
26
}
Copied!
Export as PDF
Copy link