forked from firebase/snippets-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage_users.js
175 lines (163 loc) · 4.8 KB
/
manage_users.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use strict';
const { initializeApp } = require('firebase-admin/app');
const { getAuth } = require('firebase-admin/auth');
initializeApp();
const uid = 'some_uid_1234';
const uid1 = 'some_uid_1';
const uid2 = 'some_uid_2';
const uid3 = 'some_uid_3';
const email = '[email protected]';
const phoneNumber = '+15558675309';
// [START get_user_by_id]
getAuth()
.getUser(uid)
.then((userRecord) => {
// See the UserRecord reference doc for the contents of userRecord.
console.log(`Successfully fetched user data: ${userRecord.toJSON()}`);
})
.catch((error) => {
console.log('Error fetching user data:', error);
});
// [END get_user_by_id]
// [START get_user_by_email]
getAuth()
.getUserByEmail(email)
.then((userRecord) => {
// See the UserRecord reference doc for the contents of userRecord.
console.log(`Successfully fetched user data: ${userRecord.toJSON()}`);
})
.catch((error) => {
console.log('Error fetching user data:', error);
});
// [END get_user_by_email]
// [START get_user_by_phone]
getAuth()
.getUserByPhoneNumber(phoneNumber)
.then((userRecord) => {
// See the UserRecord reference doc for the contents of userRecord.
console.log(`Successfully fetched user data: ${userRecord.toJSON()}`);
})
.catch((error) => {
console.log('Error fetching user data:', error);
});
// [END get_user_by_phone]
// [START bulk_get_users]
getAuth()
.getUsers([
{ uid: 'uid1' },
{ email: '[email protected]' },
{ phoneNumber: '+15555550003' },
{ providerId: 'google.com', providerUid: 'google_uid4' },
])
.then((getUsersResult) => {
console.log('Successfully fetched user data:');
getUsersResult.users.forEach((userRecord) => {
console.log(userRecord);
});
console.log('Unable to find users corresponding to these identifiers:');
getUsersResult.notFound.forEach((userIdentifier) => {
console.log(userIdentifier);
});
})
.catch((error) => {
console.log('Error fetching user data:', error);
});
// [END bulk_get_users]
// [START create_user]
getAuth()
.createUser({
email: '[email protected]',
emailVerified: false,
phoneNumber: '+11234567890',
password: 'secretPassword',
displayName: 'John Doe',
photoURL: 'http://www.example.com/12345678/photo.png',
disabled: false,
})
.then((userRecord) => {
// See the UserRecord reference doc for the contents of userRecord.
console.log('Successfully created new user:', userRecord.uid);
})
.catch((error) => {
console.log('Error creating new user:', error);
});
// [END create_user]
// [START create_user_with_uid]
getAuth()
.createUser({
uid: 'some-uid',
email: '[email protected]',
phoneNumber: '+11234567890',
})
.then((userRecord) => {
// See the UserRecord reference doc for the contents of userRecord.
console.log('Successfully created new user:', userRecord.uid);
})
.catch((error) => {
console.log('Error creating new user:', error);
});
// [END create_user_with_uid]
// [START update_user]
getAuth()
.updateUser(uid, {
email: '[email protected]',
phoneNumber: '+11234567890',
emailVerified: true,
password: 'newPassword',
displayName: 'Jane Doe',
photoURL: 'http://www.example.com/12345678/photo.png',
disabled: true,
})
.then((userRecord) => {
// See the UserRecord reference doc for the contents of userRecord.
console.log('Successfully updated user', userRecord.toJSON());
})
.catch((error) => {
console.log('Error updating user:', error);
});
// [END update_user]
// [START delete_user]
getAuth()
.deleteUser(uid)
.then(() => {
console.log('Successfully deleted user');
})
.catch((error) => {
console.log('Error deleting user:', error);
});
// [END delete_user]
// [START bulk_delete_users]
getAuth()
.deleteUsers([uid1, uid2, uid3])
.then((deleteUsersResult) => {
console.log(`Successfully deleted ${deleteUsersResult.successCount} users`);
console.log(`Failed to delete ${deleteUsersResult.failureCount} users`);
deleteUsersResult.errors.forEach((err) => {
console.log(err.error.toJSON());
});
})
.catch((error) => {
console.log('Error deleting users:', error);
});
// [END bulk_delete_users]
// [START list_all_users]
const listAllUsers = (nextPageToken) => {
// List batch of users, 1000 at a time.
getAuth()
.listUsers(1000, nextPageToken)
.then((listUsersResult) => {
listUsersResult.users.forEach((userRecord) => {
console.log('user', userRecord.toJSON());
});
if (listUsersResult.pageToken) {
// List next batch of users.
listAllUsers(listUsersResult.pageToken);
}
})
.catch((error) => {
console.log('Error listing users:', error);
});
};
// Start listing users from the beginning, 1000 at a time.
listAllUsers();
// [END list_all_users]