-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathset-admin.js
103 lines (87 loc) · 2.81 KB
/
set-admin.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
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
103
const admin = require('firebase-admin');
const fs = require('fs');
const existsSync = fs.existsSync;
const readFileSync = fs.readFileSync;
// This script sets or removes an admin custom claim on the firebase user of the provided email
// from https://github.com/prescottprue/cypress-firebase/
function readJsonFile(filePath) {
if (!existsSync(filePath)) {
return {};
}
try {
const fileBuffer = readFileSync(filePath, 'utf8');
return JSON.parse(fileBuffer.toString());
} catch (err) {
/* eslint-disable no-console */
console.error(
`cypress-firebase: Unable to parse ${filePath.replace(
process.cwd(),
'',
)} - JSON is most likely not valid`,
);
/* eslint-enable no-console */
return {};
}
}
// modified from https://github.com/prescottprue/cypress-firebase/
function getServiceAccount() {
const serviceAccountPath = `${process.cwd()}/serviceAccount.json`;
// Check for local service account file (Local dev)
if (existsSync(serviceAccountPath)) {
return readJsonFile(serviceAccountPath); // eslint-disable-line global-require, import/no-dynamic-require
}
// Use environment variables
const serviceAccountEnvVar = process.env.SERVICE_ACCOUNT;
if (serviceAccountEnvVar) {
if (typeof serviceAccountEnvVar === 'string') {
try {
return JSON.parse(serviceAccountEnvVar);
} catch (err) {
console.warn(
`Issue parsing 'SERVICE_ACCOUNT' environment variable from string to object, returning string`,
);
}
}
return serviceAccountEnvVar;
}
return null;
}
const args = process.argv.slice(2);
const email = args[0];
if(!email) {
throw new Error('No email provided!');
}
if(args[1] !== 'add' && args[1] !== 'remove') {
throw new Error('Invalid add/remove argument');
}
const addAdmin = args[1] === 'add'
const serviceAccount = getServiceAccount();
const projectId = process.env.GCLOUD_PROJECT || serviceAccount.project_id;
const app = admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: `https://${projectId}.firebaseio.com`
});
admin
.auth()
.getUserByEmail(email)
.then((user) => {
console.log(`Found User ID ${user.uid} with email ${user.email}`);
// Confirm user is verified.
if (user.emailVerified && addAdmin) {
// Add custom claims for additional privileges.
// This will be picked up by the user on token refresh or next sign in on new device.
console.log(`User email is verified, setting admin claim`);
return admin.auth().setCustomUserClaims(user.uid, {
admin: true,
});
} else {
console.log(`Removing admin claim.`);
return admin.auth().setCustomUserClaims(user.uid, null);
}
})
.catch((error) => {
console.log(error);
process.exit(1);
}).then(() => {
process.exit(0);
});