-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelete.js
More file actions
102 lines (92 loc) · 3.79 KB
/
Delete.js
File metadata and controls
102 lines (92 loc) · 3.79 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
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
async function remove(id, callback) {
// This script remove a user from your existing database.
// It is executed whenever a user is deleted from the API or Auth0 dashboard.
//
// There are two ways that this script can finish:
// 1. The user was removed successfully:
// callback(null);
// 2. Something went wrong while trying to reach your database:
// callback(new Error("my error message"));
const axios = require('axios');
var ManagementClient = require('auth0@2.17.0').ManagementClient;
var _ = require('lodash');
/*** Get Access Token via CC flow and pass it to the API in the header ***/
async function getAccessToken() {
if (configuration.AT_TOKEN &&
configuration.AT_TOKEN_RENEW_AT &&
configuration.AT_TOKEN_RENEW_AT > Date.now()) {
console.log("Access token is valid...cache hit!");
return configuration.AT_TOKEN;
}
else {
console.log("Access token expired or not found...cache miss!, getting new token");
const auth0LoginOpts = {
url: configuration.MANAGEMENT_TOKEN_URL,
method: "POST",
json: true,
data: {
grant_type: "client_credentials",
client_id: configuration.CLIENT_ID,
client_secret: configuration.CLIENT_SECRET,
audience: configuration.MANAGEMENT_API_AUDIENCE
}
};
console.log("getting token for ManagementClient");
const auth0LoginBody = await axios(auth0LoginOpts);
//console.log("auth0LoginBody: ",auth0LoginBody);
let management = new ManagementClient({
token: auth0LoginBody.data.access_token,
domain: configuration.AUTH0_DOMAIN
});
const localDBScriptsLoginOpts = {
url: configuration.SELF_TOKEN_URL,
method: "POST",
json: true,
data: {
grant_type: "client_credentials",
client_id: configuration.CLIENT_ID,
client_secret: configuration.CLIENT_SECRET,
audience: configuration.SELF_API_AUDIENCE
}
};
console.log("getting token for self app");
const selfLoginBody = await axios(localDBScriptsLoginOpts);
//console.log(selfLoginBody);
const connections = await management.connections.getAll();
var connection = _.find(connections, { name: configuration.CONNECTION_NAME, strategy: 'auth0' });
if (connection) {
console.log("Found connection: " + connection.name + ". Updating...");
var subset = _.pick(connection, ['options']);
subset.options.bareConfiguration = {};
subset.options.bareConfiguration.at_token = selfLoginBody.data.access_token;
// expires_in is in milliseconds, but leave a 20% buffer on refresh time just in case
subset.options.bareConfiguration.at_token_renew_at = (Date.now() + (800 * selfLoginBody.data.expires_in)).toString();
var identity = _.pick(connection, ['id']);
await management.connections.update(identity, subset);
return selfLoginBody.data.access_token;
}
}
}
/*************************************************************************/
const token = await getAccessToken();
//console.log("Token to send to api :",token);
const deleteUserOptions = {
url: configuration.API_ENDPOINT,
method: "DELETE",
json: true,
data: {
email: id
},
headers: {
"Authorization": "Bearer " + token
}
};
await axios(deleteUserOptions)
.then(function (response) {
return callback(null);
})
.catch(function (error) {
//console.log(error);
return callback(new Error(error));
});
}