|
| 1 | +'use strict'; |
| 2 | +const admin = require('firebase-admin'); |
| 3 | +admin.initializeApp(); |
| 4 | + |
| 5 | +//[START build_user_list] |
| 6 | +// Up to 1000 users can be imported at once. |
| 7 | +var userImportRecords = [ |
| 8 | + { |
| 9 | + uid: 'uid1', |
| 10 | + |
| 11 | + passwordHash: Buffer.from('passwordHash1'), |
| 12 | + passwordSalt: Buffer.from('salt1') |
| 13 | + }, |
| 14 | + { |
| 15 | + uid: 'uid2', |
| 16 | + |
| 17 | + passwordHash: Buffer.from('passwordHash2'), |
| 18 | + passwordSalt: Buffer.from('salt2') |
| 19 | + }, |
| 20 | + //... |
| 21 | +]; |
| 22 | +//[END build_user_list] |
| 23 | + |
| 24 | +var userImportOptions = { |
| 25 | + hash: { |
| 26 | + algorithm: 'HMAC_SHA256', |
| 27 | + key: Buffer.from('secretKey') |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +// [START import_users] |
| 32 | +admin.auth().importUsers(userImportRecords, userImportOptions) |
| 33 | + .then(function(userImportResult) { |
| 34 | + // The number of successful imports is determined via: userImportResult.successCount. |
| 35 | + // The number of failed imports is determined via: userImportResult.failureCount. |
| 36 | + // To get the error details. |
| 37 | + userImportResult.forEach(function(indexedError) { |
| 38 | + // The corresponding user that failed to upload. |
| 39 | + console.log(userImportRecords[indexedError.index].uid +' failed to import', |
| 40 | + indexedError.error); |
| 41 | + }); |
| 42 | + }) |
| 43 | + .catch(function(error) { |
| 44 | + // Some unrecoverable error occurred that prevented the operation from running. |
| 45 | + }); |
| 46 | +// [END import_users] |
| 47 | + |
| 48 | +// [START import_with_hmac] |
| 49 | +admin.auth().importUsers([{ |
| 50 | + uid: 'some-uid', |
| 51 | + |
| 52 | + // Must be provided in a byte buffer. |
| 53 | + passwordHash: Buffer.from('password-hash'), |
| 54 | + // Must be provided in a byte buffer. |
| 55 | + passwordSalt: Buffer.from('salt') |
| 56 | +}], { |
| 57 | + hash: { |
| 58 | + algorithm: 'HMAC_SHA256', |
| 59 | + // Must be provided in a byte buffer. |
| 60 | + key: Buffer.from('secret') |
| 61 | + } |
| 62 | +}).then(function(results) { |
| 63 | + results.errors.forEach(function(indexedError) { |
| 64 | + console.log('Error importing user ' + indexedError.index); |
| 65 | + }); |
| 66 | +}).catch(function(error) { |
| 67 | + console.log('Error importing users:', error); |
| 68 | +}); |
| 69 | +// [END import_with_hmac] |
| 70 | + |
| 71 | +// [START import_with_pbkdf] |
| 72 | +admin.auth().importUsers([{ |
| 73 | + uid: 'some-uid', |
| 74 | + |
| 75 | + // Must be provided in a byte buffer. |
| 76 | + passwordHash: Buffer.from('password-hash'), |
| 77 | + // Must be provided in a byte buffer. |
| 78 | + passwordSalt: Buffer.from('salt') |
| 79 | +}], { |
| 80 | + hash: { |
| 81 | + algorithm: 'PBKDF2_SHA256', |
| 82 | + rounds: 100000 |
| 83 | + } |
| 84 | +}).then(function(results) { |
| 85 | + results.errors.forEach(function(indexedError) { |
| 86 | + console.log('Error importing user ' + indexedError.index); |
| 87 | + }); |
| 88 | +}).catch(function(error) { |
| 89 | + console.log('Error importing users:', error); |
| 90 | +}); |
| 91 | +// [END import_with_pbkdf] |
| 92 | + |
| 93 | +// [START import_with_standard_scrypt] |
| 94 | +admin.auth().importUsers([{ |
| 95 | + uid: 'some-uid', |
| 96 | + |
| 97 | + // Must be provided in a byte buffer. |
| 98 | + passwordHash: Buffer.from('password-hash'), |
| 99 | + // Must be provided in a byte buffer. |
| 100 | + passwordSalt: Buffer.from('salt') |
| 101 | +}], { |
| 102 | + hash: { |
| 103 | + algorithm: 'STANDARD_SCRYPT', |
| 104 | + memoryCost: 1024, |
| 105 | + parallelization: 16, |
| 106 | + blockSize: 8, |
| 107 | + derivedKeyLength: 64 |
| 108 | + } |
| 109 | +}).then(function(results) { |
| 110 | + results.errors.forEach(function(indexedError) { |
| 111 | + console.log('Error importing user ' + indexedError.index); |
| 112 | + }); |
| 113 | +}).catch(function(error) { |
| 114 | + console.log('Error importing users:', error); |
| 115 | +}); |
| 116 | +// [END import_with_standard_scrypt] |
| 117 | + |
| 118 | +// [START import_with_bcrypt] |
| 119 | +admin.auth().importUsers([{ |
| 120 | + uid: 'some-uid', |
| 121 | + |
| 122 | + // Must be provided in a byte buffer. |
| 123 | + passwordHash: Buffer.from('password-hash') |
| 124 | +}], { |
| 125 | + hash: { |
| 126 | + algorithm: 'BCRYPT' |
| 127 | + } |
| 128 | +}).then(function(results) { |
| 129 | + results.errors.forEach(function(indexedError) { |
| 130 | + console.log('Error importing user ' + indexedError.index); |
| 131 | + }); |
| 132 | +}).catch(function(error) { |
| 133 | + console.log('Error importing users:', error); |
| 134 | +}); |
| 135 | +// [END import_with_bcrypt] |
| 136 | + |
| 137 | + |
| 138 | +// [START import_with_scrypt] |
| 139 | +admin.auth().importUsers([{ |
| 140 | + uid: 'some-uid', |
| 141 | + |
| 142 | + // Must be provided in a byte buffer. |
| 143 | + passwordHash: Buffer.from('base64-password-hash', 'base64'), |
| 144 | + // Must be provided in a byte buffer. |
| 145 | + passwordSalt: Buffer.from('base64-salt', 'base64') |
| 146 | +}], { |
| 147 | + hash: { |
| 148 | + algorithm: 'SCRYPT', |
| 149 | + // All the parameters below can be obtained from the Firebase Console's users section. |
| 150 | + // Must be provided in a byte buffer. |
| 151 | + key: Buffer.from('base64-secret', 'base64'), |
| 152 | + saltSeparator: Buffer.from('base64SaltSeparator', 'base64'), |
| 153 | + rounds: 8, |
| 154 | + memoryCost: 14 |
| 155 | + } |
| 156 | +}).then(function(results) { |
| 157 | + results.errors.forEach(function(indexedError) { |
| 158 | + console.log('Error importing user ' + indexedError.index); |
| 159 | + }); |
| 160 | +}).catch(function(error) { |
| 161 | + console.log('Error importing users:', error); |
| 162 | +}); |
| 163 | +// [END import_with_scrypt] |
| 164 | + |
| 165 | +// [START import_without_password] |
| 166 | +admin.auth().importUsers([{ |
| 167 | + uid: 'some-uid', |
| 168 | + displayName: 'John Doe', |
| 169 | + |
| 170 | + photoURL: 'http://www.example.com/12345678/photo.png', |
| 171 | + emailVerified: true, |
| 172 | + phoneNumber: '+11234567890', |
| 173 | + // Set this user as admin. |
| 174 | + customClaims: {admin: true}, |
| 175 | + // User with Google provider. |
| 176 | + providerData: [{ |
| 177 | + uid: 'google-uid', |
| 178 | + |
| 179 | + displayName: 'John Doe', |
| 180 | + photoURL: 'http://www.example.com/12345678/photo.png', |
| 181 | + providerId: 'google.com' |
| 182 | + }] |
| 183 | +}]).then(function(results) { |
| 184 | + results.errors.forEach(function(indexedError) { |
| 185 | + console.log('Error importing user ' + indexedError.index); |
| 186 | + }); |
| 187 | +}).catch(function(error) { |
| 188 | + console.log('Error importing users:', error); |
| 189 | +}); |
| 190 | +// [END import_without_password] |
| 191 | + |
| 192 | + |
| 193 | + |
0 commit comments