-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-roles-users.js
77 lines (65 loc) · 2 KB
/
add-roles-users.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
require("dotenv").config()
const fetch = require("node-fetch")
const fs = require("fs")
// tipos de usuario
let admins = [] // id rol 1
let pas = [] // id rol 2
const adminRoleId = "rol_33f0f1rAvQjFj0ES"
const pasRoleId = "rol_26G1WmyNAf1Cp7ky"
// MAIN
getUsers()
.then(users => {
fs.writeFileSync("auth0-updated-users.json", JSON.stringify(users))
console.log("saved users to json.")
admins = getUsersWithRole(users, 1)
pas = getUsersWithRole(users, 2)
// console.log("admins:", admins)
// console.log("pas:", pas)
const body = {
users: []
}
body.users = admins
setUsersToRole(JSON.stringify(body), adminRoleId)
.then(res => console.log("set users to role admin", res))
.catch(err => console.error("set users to role admin err", err))
body.users = pas
setUsersToRole(JSON.stringify(body), pasRoleId)
.then(res => console.log("set users to role pas", res))
.catch(err => console.error("set users to role pas err", err))
})
.catch(e => console.error("error al get users", e))
// get users from auth0
async function getUsers() {
const users = await fetch(`${process.env.AUTH0_HOST}/api/v2/users`,
{
method: "GET",
headers: {
"Authorization": "Bearer " + process.env.AUTH0_TOKEN,
"Content-Type": "application/json"
}
})
const usersJSON = await users.json()
return usersJSON
}
// create array of users id with specified rol
function getUsersWithRole(users, rol) {
const usersWithRole = users.filter(u => {
return u.app_metadata.rol === rol
})
return usersWithRole.map(u => u.user_id)
}
// set rol to specified users
async function setUsersToRole(users, rol) {
const response = await fetch(`${process.env.AUTH0_HOST}/api/v2/roles/${rol}/users`,
{
method: "POST",
headers: {
"Authorization": "Bearer " + process.env.AUTH0_TOKEN,
"Content-Type": "application/json"
},
body: users
}
)
const resJson = await response.json()
return resJson
}