Skip to content

Commit bd4a65b

Browse files
committed
Created README
1 parent b6c6083 commit bd4a65b

File tree

1 file changed

+203
-0
lines changed

1 file changed

+203
-0
lines changed

admin-functions/README.md

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
# admin-functions
2+
3+
Create, delete and list all users
4+
5+
### Using firestore trigger
6+
7+
* ##### Create user
8+
9+
```javascript
10+
exports.createUser = functions.firestore
11+
.document('User/{userId}')
12+
.onCreate((snap) => {
13+
//you can add any other property present in
14+
//https://firebase.google.com/docs/reference/js/firebase.User#properties
15+
let dbvalues = snap.data();
16+
let name = dbvalues.name;
17+
let pass = dbvalues.pass;
18+
let email = dbvalues.email;
19+
//create user using data of recently add user on firestore
20+
admin.auth().createUser({
21+
email: email,
22+
password: pass,
23+
displayName: name
24+
})
25+
.then(userRecord => {
26+
//Get the uid of the user newly created and adds to the document
27+
//referring to the user, this step is very important so that the
28+
//trigger used to delete the user works
29+
snap.data.ref.set({
30+
uid: userRecord.uid
31+
}, {
32+
merge: true
33+
});
34+
//delete user pass field
35+
snap.data.ref.update({
36+
pass: FieldValue.delete()
37+
});
38+
console.log("Successfully created new user: ", userRecord);
39+
})
40+
.catch((error) => {
41+
console.log("There was an error creating the user: ", error);
42+
});
43+
});
44+
```
45+
* ##### Delete user
46+
```javascript
47+
exports.deleteUser = functions.firestore
48+
.document('User/{userId}')
49+
.onDelete((snap) => {
50+
//get delete document data
51+
let dbvalues = snap.data();
52+
let uid = dbvalues.uid;
53+
//revoke login token for desconect a conected user
54+
admin.auth().revokeRefreshTokens(uid)
55+
.then(() => {
56+
//delete user using uid present on document
57+
admin.auth().deleteUser(uid)
58+
.then(() => {
59+
console.log("Deleted user successfully: ", uid);
60+
})
61+
.catch(error => {
62+
console.log("There was an error deleting the user: ", error);
63+
});
64+
});
65+
});
66+
```
67+
### Using https request
68+
69+
* ##### Create user
70+
```javascript
71+
exports.createUser = functions.https.onRequest((req, res) => {
72+
cors(req, res, () => {
73+
//get user token to verify that the user is actually logged in
74+
const userToken = req.get('userToken');
75+
//verify the token and returns DecodedIdToken as decoded, containing the
76+
//list properties here
77+
//https://firebase.google.com/docs/reference/admin/node/admin.auth.DecodedIdToken
78+
return admin.auth().verifyIdToken(userToken)
79+
.then((decoded) => {
80+
//create user using data received by a post request, you can see
81+
//more on http://expressjs.com/en/4x/api.html#req.body
82+
admin.auth().createUser({
83+
//you can add any other property present in
84+
//https://firebase.google.com/docs/reference/js/firebase.User#properties
85+
email: req.body.email,
86+
password: req.body.pass,
87+
displayName: req.body.name,
88+
})
89+
.then((userRecord) => {
90+
console.log("Successfully created new user:", userRecord.uid);
91+
//send a message to client web if the user is
92+
//successfully created
93+
res.status(200).send({
94+
code: 1,
95+
message: 'Successfully created new user',
96+
userRecord: userRecord
97+
});
98+
})
99+
.catch((error) => {
100+
console.log("Error creating new user:", error);
101+
//send a message to client web if occurred an error on
102+
//user creation
103+
res.status(400).send({
104+
code: 2,
105+
message: 'Erro ao criar usuario',
106+
error: error
107+
});
108+
});
109+
}) //send a message to client web if the user token is invalid
110+
.catch((err) => res.status(401).send(err));
111+
});
112+
});
113+
```
114+
* ##### Delete user
115+
```javascript
116+
exports.deleteUser = functions.https.onRequest((req, res) => {
117+
cors(req, res, () => {
118+
//get user token to verify that the user is actually logged in
119+
const userToken = req.get('userToken');
120+
//verify the token and returns DecodedIdToken as decoded, containing the
121+
//list properties here
122+
//https://firebase.google.com/docs/reference/admin/node/admin.auth.DecodedIdToken
123+
return admin.auth().verifyIdToken(userToken)
124+
.then((decoded) => {
125+
let uid = req.body.uid;
126+
//revoke login token for desconect a conected user
127+
admin.auth().revokeRefreshTokens(uid)
128+
.then(() => {
129+
//delete user using uid received by a post request and
130+
//attributed to "let uid = req.body.uid", you can see
131+
//more on http://expressjs.com/en/4x/api.html#req.body
132+
admin.auth().deleteUser(uid)
133+
.then(() => {
134+
console.log("Deleted user successfully");
135+
//send a message to client web if the user is
136+
//successfully deleted
137+
res.status(200).send({
138+
code: 1,
139+
message: 'Deleted user successfully'
140+
});
141+
})
142+
.catch((error) => {
143+
console.log("There was an error deleting the user", error);
144+
//send a message to client web if occurred an
145+
//error on user delete
146+
res.status(400).send({
147+
code: 2,
148+
message: 'There was an error deleting the user',
149+
error: error
150+
});
151+
});
152+
});
153+
154+
}) //send a message to client web if the user token is invalid
155+
.catch((err) => res.status(401).send(err));
156+
});
157+
});
158+
```
159+
* ##### List all users
160+
```javascript
161+
exports.listUsers = functions.https.onRequest((req, res) => {
162+
cors(req, res, () => {
163+
//get user token to verify that the user is actually logged in
164+
const userToken = req.get('userToken');
165+
//verify the token and returns DecodedIdToken as decoded, containing the
166+
//list properties here
167+
//https://firebase.google.com/docs/reference/admin/node/admin.auth.DecodedIdToken
168+
return admin.auth().verifyIdToken(userToken)
169+
.then((decoded) => {
170+
//list all users and return listUsersResult, you can see
171+
//complete properties here
172+
//https://firebase.google.com/docs/reference/admin/node/admin.auth.ListUsersResult
173+
admin.auth().listUsers().then((listUsersResult) => {
174+
//in this part we assign the "users" property of the
175+
//listUsersResult to the variable users, you can see more of
176+
//users properties here
177+
//https://firebase.google.com/docs/reference/admin/node/admin.auth.UserRecord
178+
let users = listUsersResult.users;
179+
let objUsers = [];
180+
//create an array of objects with information you want to
181+
//return
182+
for (let i = 0; i < users.length; i++) {
183+
//here we exclude the admin user from being listed
184+
if (users[i]['email'] !== 'rifadasorte2@gmail.com') {
185+
objUsers[i] = {
186+
displayName: users[i]['displayName'],
187+
uid: users[i]['uid'],
188+
email: users[i]['email']
189+
};
190+
}
191+
}
192+
console.log(objUsers.filter(Boolean));
193+
//as i had used an "if" to exclude the admin user from the
194+
//listing, the "objUser" array would return with one of its
195+
//positions as null, to solve this we used the filter
196+
//function
197+
res.status(200).send(objUsers.filter(Boolean));
198+
});
199+
}) //send a message to client web if the user token is invalid
200+
.catch((err) => res.status(401).send(err));
201+
});
202+
});
203+
```

0 commit comments

Comments
 (0)