1 | const Joi = require('@hapi/joi');
|
2 | const base64 = require('js-base64').Base64;
|
3 |
|
4 | const _instance = Joi.string();
|
5 | const _email = Joi.string().email();
|
6 | const _password = Joi.string().min(1);
|
7 | const _token = Joi.string().min(1);
|
8 |
|
9 | module.exports = (options = {}) => {
|
10 | const { error } = Joi.object({
|
11 | instance: _instance,
|
12 | email: _email.required(),
|
13 | password: _password,
|
14 | token: _token
|
15 | })
|
16 | .xor('password', 'token')
|
17 | .validate(options);
|
18 | if (error) throw new Error(error.details[0].message);
|
19 |
|
20 |
|
21 | const { email, password, token } = options;
|
22 | const auth = token ? `${email}/token:${token}` : `${email}:${password}`;
|
23 | const encoded = base64.encode(auth);
|
24 |
|
25 |
|
26 | return {
|
27 | 'Content-Type': 'application/json',
|
28 | Authorization: `Basic ${encoded}`
|
29 | };
|
30 | };
|