This repository has been archived by the owner on Feb 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
177 lines (155 loc) · 3.71 KB
/
index.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
API request library.
*/
import axios from 'axios';
import jwt_decode from 'jwt-decode';
import router from '@/router';
let { hostname, port } = window.location;
// shared access token for all APIs
let accessToken = null;
let tokenClaims = {};
// change parameters for development since it uses CORS
if (process.env.NODE_ENV !== 'production') {
port = 9090;
axios.defaults.withCredentials = true;
}
axios.defaults.baseURL = `https://${hostname}:${port}`;
/**
* Request an access token and stores it in the file variable `accessToken`.
*
* @param {bool} force - Set true if a brand new access token should be
* requested rather than using the currently saved one.
* @returns {Promise<null>}
*/
function getAccessToken(force) {
// If an access token already exists, do nothing.
if (accessToken && !force) {
return new Promise((resolve) => resolve(null));
}
return axios({
method: 'get',
url: `/api/auth/access`,
}).then((result) => {
accessToken = result.data;
tokenClaims = jwt_decode(result.data) || {};
return null;
});
}
/**
* Log out of the system
* @returns {Promise<null>}
*/
function doLogout() {
return axios({
method: 'post',
url: `/api/auth/logout`,
}).then(() => {
accessToken = '';
tokenClaims = {};
router.push('/login').catch((e) => {
if (e.name != 'NavigationDuplicated') {
throw e;
}
});
return null;
});
}
axios.interceptors.request.use(
(config) => {
if (!config.url.startsWith('/api/auth/') && config.url !== '/api/login') {
// Add authorization header for non-auth related APIs
config.headers.authorization = `Bearer ${accessToken}`;
}
return config;
},
null,
);
axios.interceptors.response.use(
null,
async (e) => {
// Rethrow any non-authorization errors
if (!e.response || e.response.status !== 403) {
throw e;
}
const { config } = e;
// If the original request was related to authorization already, logout and exit early.
if (config.url.startsWith('/api/auth/')) {
await doLogout();
throw e;
}
// Get the new access token
await getAccessToken(true);
// Resend the original request. Wrap in try/catch so that it only tries
// once and does not keep repeating.
return await axios.request(config);
},
{ synchronous: true },
);
export default {
/**
* Attempt login.
* @returns {Promise<null>}
*/
login(email, pw) {
return axios({
method: 'post',
url: `/api/login`,
data: { email, pw },
}).then(this.access);
},
/**
* Request API access.
* @returns {Promise<null>}
*/
access() {
return getAccessToken();
},
/**
* Get the error message
* @param {Object} e - Error response from an API.
* @returns {string}
*/
getErrorMsg(e) {
// e.response.data.msg is for custom rejections
// e.response.data is for default Warp rejections
// e.message is the default error message for the error code
return e.response && (e.response.data.msg || e.response.data) || e.message;
},
/**
* Call the user API.
* @returns {Promise<String>}
*/
callUser() {
return axios({
method: 'post',
url: `/api/user`,
}).then((resp) => resp.data);
},
/**
* Call the user API
* @returns {Promise<String>}
*/
callAdmin() {
return axios({
method: 'post',
url: `/api/admin`,
}).then((resp) => resp.data);
},
/**
* Get the claims.
* @returns {Promise<Object>}
*/
claims() {
if (accessToken) {
return new Promise((resolve) => resolve(tokenClaims));
}
return this.access().then(() => tokenClaims);
},
/**
* Log out.
* @returns {Promise<null>}
*/
logout() {
doLogout();
}
}