Skip to content

Commit f688622

Browse files
committed
Wrap lines
1 parent 03828e4 commit f688622

File tree

1 file changed

+49
-24
lines changed

1 file changed

+49
-24
lines changed

admin-functions/functions/index.js

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ admin.initializeApp();
55
const cors = require('cors')({
66
origin: '' //set your especific domain for cors or true for allow any domain
77
});
8-
/*
8+
99
//if you prefer to use a firestore trigger
1010
//for create / delete account using admin sdk on web client
1111
//create by firestore trigger
@@ -24,7 +24,9 @@ exports.createUser = functions.firestore
2424
displayName: name
2525
})
2626
.then(userRecord => {
27-
//Get the uid of the user newly created and adds to the document referring to the user, this step is very important so that the trigger used to delete the user works
27+
//Get the uid of the user newly created and adds to the document
28+
//referring to the user, this step is very important so that the
29+
//trigger used to delete the user works
2830
snap.data.ref.set({
2931
uid: userRecord.uid
3032
}, {
@@ -60,26 +62,31 @@ exports.deleteUser = functions.firestore
6062
});
6163
});
6264
});
63-
*/
65+
6466
//http functions
6567
//create user
6668
exports.createUser = functions.https.onRequest((req, res) => {
6769
cors(req, res, () => {
6870
//get user token to verify that the user is actually logged in
6971
const userToken = req.get('userToken');
70-
//verify the token and returns DecodedIdToken as decoded, containing the list properties here https://firebase.google.com/docs/reference/admin/node/admin.auth.DecodedIdToken
72+
//verify the token and returns DecodedIdToken as decoded, containing the
73+
//list properties here
74+
//https://firebase.google.com/docs/reference/admin/node/admin.auth.DecodedIdToken
7175
return admin.auth().verifyIdToken(userToken)
7276
.then((decoded) => {
73-
//create user using data received by a post request, you can see more on http://expressjs.com/en/4x/api.html#req.body
77+
//create user using data received by a post request, you can see
78+
//more on http://expressjs.com/en/4x/api.html#req.body
7479
admin.auth().createUser({
75-
//you can add any other property present in https://firebase.google.com/docs/reference/js/firebase.User#properties
80+
//you can add any other property present in
81+
//https://firebase.google.com/docs/reference/js/firebase.User#properties
7682
email: req.body.email,
7783
password: req.body.pass,
7884
displayName: req.body.name,
7985
})
8086
.then((userRecord) => {
8187
console.log("Successfully created new user:", userRecord.uid);
82-
//send a message to client web if the user is successfully created
88+
//send a message to client web if the user is
89+
//successfully created
8390
res.status(200).send({
8491
code: 1,
8592
message: 'Successfully created new user',
@@ -88,42 +95,49 @@ exports.createUser = functions.https.onRequest((req, res) => {
8895
})
8996
.catch((error) => {
9097
console.log("Error creating new user:", error);
91-
//send a message to client web if occurred an error on user creation
98+
//send a message to client web if occurred an error on
99+
//user creation
92100
res.status(400).send({
93101
code: 2,
94102
message: 'Erro ao criar usuario',
95103
error: error
96104
});
97105
});
98-
})
99-
.catch((err) => res.status(401).send(err)); //send a message to client web if the user token is invalid
106+
}) //send a message to client web if the user token is invalid
107+
.catch((err) => res.status(401).send(err));
100108
});
101109
});
102110
//delete user
103111
exports.deleteUser = functions.https.onRequest((req, res) => {
104112
cors(req, res, () => {
105113
//get user token to verify that the user is actually logged in
106114
const userToken = req.get('userToken');
107-
//verify the token and returns DecodedIdToken as decoded, containing the list properties here https://firebase.google.com/docs/reference/admin/node/admin.auth.DecodedIdToken
115+
//verify the token and returns DecodedIdToken as decoded, containing the
116+
//list properties here
117+
//https://firebase.google.com/docs/reference/admin/node/admin.auth.DecodedIdToken
108118
return admin.auth().verifyIdToken(userToken)
109119
.then((decoded) => {
110120
let uid = req.body.uid;
111121
//revoke login token for desconect a conected user
112122
admin.auth().revokeRefreshTokens(uid)
113123
.then(() => {
114-
//delete user using uid received by a post request and attributed to "let uid = req.body.uid", you can see more on http://expressjs.com/en/4x/api.html#req.body
124+
//delete user using uid received by a post request and
125+
//attributed to "let uid = req.body.uid", you can see
126+
//more on http://expressjs.com/en/4x/api.html#req.body
115127
admin.auth().deleteUser(uid)
116128
.then(() => {
117129
console.log("Deleted user successfully");
118-
//send a message to client web if the user is successfully deleted
130+
//send a message to client web if the user is
131+
//successfully deleted
119132
res.status(200).send({
120133
code: 1,
121134
message: 'Deleted user successfully'
122135
});
123136
})
124137
.catch((error) => {
125138
console.log("There was an error deleting the user", error);
126-
//send a message to client web if occurred an error on user delete
139+
//send a message to client web if occurred an
140+
//error on user delete
127141
res.status(400).send({
128142
code: 2,
129143
message: 'There was an error deleting the user',
@@ -132,24 +146,32 @@ exports.deleteUser = functions.https.onRequest((req, res) => {
132146
});
133147
});
134148

135-
})
136-
.catch((err) => res.status(401).send(err)); //send a message to client web if the user token is invalid
149+
}) //send a message to client web if the user token is invalid
150+
.catch((err) => res.status(401).send(err));
137151
});
138152
});
139153
//list all user
140154
exports.listUsers = functions.https.onRequest((req, res) => {
141155
cors(req, res, () => {
142156
//get user token to verify that the user is actually logged in
143157
const userToken = req.get('userToken');
144-
//verify the token and returns DecodedIdToken as decoded, containing the list properties here https://firebase.google.com/docs/reference/admin/node/admin.auth.DecodedIdToken
158+
//verify the token and returns DecodedIdToken as decoded, containing the
159+
//list properties here
160+
//https://firebase.google.com/docs/reference/admin/node/admin.auth.DecodedIdToken
145161
return admin.auth().verifyIdToken(userToken)
146162
.then((decoded) => {
147-
//list all users and return listUsersResult, you can see complete properties here https://firebase.google.com/docs/reference/admin/node/admin.auth.ListUsersResult
163+
//list all users and return listUsersResult, you can see
164+
//complete properties here
165+
//https://firebase.google.com/docs/reference/admin/node/admin.auth.ListUsersResult
148166
admin.auth().listUsers().then((listUsersResult) => {
149-
//in this part we assign the "users" property of the listUsersResult to the variable users, you can see more of users properties here https://firebase.google.com/docs/reference/admin/node/admin.auth.UserRecord
167+
//in this part we assign the "users" property of the
168+
//listUsersResult to the variable users, you can see more of
169+
//users properties here
170+
//https://firebase.google.com/docs/reference/admin/node/admin.auth.UserRecord
150171
let users = listUsersResult.users;
151172
let objUsers = [];
152-
//create an array of objects with information you want to return
173+
//create an array of objects with information you want to
174+
//return
153175
for (let i = 0; i < users.length; i++) {
154176
//here we exclude the admin user from being listed
155177
if (users[i]['email'] !== 'rifadasorte2@gmail.com') {
@@ -161,10 +183,13 @@ exports.listUsers = functions.https.onRequest((req, res) => {
161183
}
162184
}
163185
console.log(objUsers.filter(Boolean));
164-
//as i had used an "if" to exclude the admin user from the listing, the "objUser" array would return with one of its positions as null, to solve this we used the filter function
186+
//as i had used an "if" to exclude the admin user from the
187+
//listing, the "objUser" array would return with one of its
188+
//positions as null, to solve this we used the filter
189+
//function
165190
res.status(200).send(objUsers.filter(Boolean));
166191
});
167-
})
168-
.catch((err) => res.status(401).send(err)); //send a message to client web if the user token is invalid
192+
}) //send a message to client web if the user token is invalid
193+
.catch((err) => res.status(401).send(err));
169194
});
170-
});
195+
});

0 commit comments

Comments
 (0)