-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew-user-auth.js
47 lines (39 loc) · 936 Bytes
/
new-user-auth.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
// This should be added to Auth0 as a Pre User Registration Hook
module.exports = (user, context, cb) => {
const admin = require("firebase-admin");
const uuid = require('uuid');
var serviceAccount = {
projectId: "FB_PROJECT_ID",
privateKey: "FB_PRIVATE_KEY",
clientEmail: "SERVICE_ACCOUNT_EMAIL"
};
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "FB_DATABASE_URL"
});
const db = admin.database(),
orgsRef = db.ref('orgs'),
orgUUID = uuid(),
usersRef = db.ref(`orgs/${orgUUID}/users`),
userUUID = uuid();
Promise.all([
usersRef.set({
[userUUID]: {
role: 'admin'
}
})
])
.then(() => {
const response = {};
response.user = {
user_metadata: {
org: orgUUID,
fbid: userUUID
}
};
cb(null, response);
})
.catch(e => {
cb(e, response);
});
};