generated from basiqio-oss/account-verification
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclientAuthentication.js
63 lines (49 loc) · 2.02 KB
/
clientAuthentication.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 { axios } from './utils/axios';
/**
* The Basiq API authentication process is fairly straight forward, we simply exchange our API key for a token which has an expiry of 60 minutes
* Our token will be passed as the authorization header to requests made to the Basiq API, which you can find in `./utils/axios`
*
* IMPORTANT: You do not want to request a new token on every request!
* In this file we keep the latest client token in local storage and only request a new token when it is expired
*
* https://api.basiq.io/reference/authentication
* */
const REFRESH_INTERVAL = 1000 * 60 * 30; // 30 minutes
const TOKEN_KEY = 'basiqApiClientAccessToken';
const REFRESH_DATE_KEY = 'basiqApiClientAccessTokenRefreshDate';
export async function getBasiqAuthorizationHeader() {
const token = await getClientToken();
return `Bearer ${token}`;
}
export async function getClientToken(userId) {
let token = getClientTokenFromLocalStorage();
const refreshDate = getClientTokenRefreshDateFromLocalStorage() || 0;
if (!token || Date.now() - refreshDate > REFRESH_INTERVAL || userId) {
// If we don't have a client token in memory or the token has expired, fetch a new one
token = await updateClientToken(userId);
}
return token;
}
async function updateClientToken(userId) {
const token = await getNewClientToken(userId);
setClientTokenInLocalStorage(token);
const refreshDate = Date.now();
setClientTokenRefreshDateInLocalStorage(refreshDate);
return token;
}
async function getNewClientToken(userId) {
const { data } = await axios.get('/api/client-token', { params: { userId } });
return data;
}
export function getClientTokenFromLocalStorage() {
return localStorage.getItem(TOKEN_KEY);
}
export function setClientTokenInLocalStorage(token) {
localStorage.setItem(TOKEN_KEY, token);
}
export function getClientTokenRefreshDateFromLocalStorage() {
return localStorage.getItem(REFRESH_DATE_KEY);
}
export function setClientTokenRefreshDateInLocalStorage(token) {
localStorage.setItem(REFRESH_DATE_KEY, token);
}