-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
71 lines (62 loc) · 2.01 KB
/
index.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
'use strict';
// load environment variables in .env
require('dotenv').config({path: `${__dirname}/.env`});
const chalk = require('chalk');
const admin = require('firebase-admin');
// env variables
const FIREBASE_SERVICE_ACCOUNT = process.env.FIREBASE_SERVICE_ACCOUNT;
// guards
if (!FIREBASE_SERVICE_ACCOUNT) {
console.error(chalk`{yellow WARN: environment variable {bold FIREBASE_SERVICE_ACCOUNT} not found!}`);
console.error(chalk`{yellow WARN: Please run {bold \`fb-admin project <PATH_TO_CRED>\`} to setup project credentials!}`);
console.error(chalk`{yellow WARN: firebase app not initialized!}`);
return;
}
try {
const cred = require(FIREBASE_SERVICE_ACCOUNT);
const {project_id} = cred;
console.log(chalk`{green INFO: CURRENT PROJECT<${project_id}>}`);
admin.initializeApp({
credential: admin.credential.cert(cred),
databaseURL: `https://${project_id}.firebaseio.com`
});
} catch (error) {
console.error(chalk`{red ERROR: ${error}}`);
return;
}
function setCustomClaims(uid, customClaims) {
// Set admin privilege on the user corresponding to uid.
return admin.auth().setCustomUserClaims(uid, customClaims);
}
function listCustomClaims(uid) {
// Lookup the user associated with the specified uid.
return admin
.auth()
.getUser(uid)
.then(userRecord => userRecord.customClaims);
}
function verifyCustomClaims(idToken, predicate) {
// Verify the ID token first.
return admin
.auth()
.verifyIdToken(idToken)
.then(claims => predicate(claims));
}
async function listAllUsers(nextPageToken, n=1000) {
// List batch of users, 1000 at a time.
var listUsersResult = await admin.auth().listUsers(n, nextPageToken);
var list = listUsersResult.users.map(function({uid, email, emailVerified, customClaims}) {
return {uid, email, emailVerified, customClaims};
});
return {list, pageToken: listUsersResult.pageToken};
}
function end() {
return admin.app().delete();
}
module.exports = {
listAllUsers,
setCustomClaims,
listCustomClaims,
verifyCustomClaims,
end
};