forked from SalesforceCommerceCloud/b2c-crm-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_b2cAuthBMUser.js
More file actions
60 lines (44 loc) · 1.76 KB
/
_b2cAuthBMUser.js
File metadata and controls
60 lines (44 loc) · 1.76 KB
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
'use strict';
// Initialize constants
const config = require('config'),
b2cAuthBMUser = require('../apis/sfcc/ocapi/auth/_bmUserGrant');
/**
* @function _b2cAuthBMUserGrant
* @description Attempts to authenticate against the B2C Commerce instance using BM User credentials --
* and returns the results of the request via the CLI.
*
* @param {Object} environmentDef Represents the already-validated environment details to use when performing the actions
* @returns {Promise} Returns the BMUserGrant authentication results
*/
module.exports = environmentDef => new Promise(async (resolve, reject) => {
// Initialize the output
const output = {
apiCalls: {
authenticate: {}
},
outputDisplay: {}
};
try {
// Execute the BM User authentication attempt
output.apiCalls.authenticate = await b2cAuthBMUser(environmentDef);
// Was an authentication error caught?
if (output.apiCalls.authenticate.status !== 200) {
// Capture the error message
output.success = false;
// Build the output display
output.outputDisplay = [
['errorType', output.apiCalls.authenticate.data.error],
['errorMessage', output.apiCalls.authenticate.data.error_description]
];
} else {
// Capture the authToken and build out the display details
output.authToken = output.apiCalls.authenticate.data.access_token;
output.success = true;
// Build the output display
output.outputDisplay = [output.apiCalls.authenticate.data.access_token];
}
resolve(output);
} catch (e) {
reject(`${config.get('errors.b2c.unableToAuthenticate')}`);
}
});