From 0fd039836f8009b63e69760d1da88283a4c8ef0a Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 31 Mar 2020 10:26:29 -0400 Subject: [PATCH 1/4] Snippets for new Auth admin features --- auth/manage_users.js | 42 +++++++++++++++++++++++++++++++++++++++ auth/tenant_management.js | 17 ++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 auth/tenant_management.js diff --git a/auth/manage_users.js b/auth/manage_users.js index f96d7861..3d3ec0f0 100644 --- a/auth/manage_users.js +++ b/auth/manage_users.js @@ -35,6 +35,17 @@ admin.auth().getUserByPhoneNumber(phoneNumber) }); // [END get_user_by_phone] +// [START get_user_by_federated_id] +admin.auth().getUserByProviderUid('google.com', 'google_uid1234') + .then(function(userRecord) { + // See the UserRecord reference doc for the contents of userRecord. + console.log('Successfully fetched user data:', userRecord.toJSON()); + }) + .catch(function(error) { + console.log('Error fetching user data:', error); + }); +// [END get_user_by_federated_id] + // [START bulk_get_users] admin.auth().getUsers([ { uid: 'uid1' }, @@ -111,6 +122,37 @@ admin.auth().updateUser(uid, { }); // [END update_user] +// [START update_user_link_federated] +// Link the user with a federated identity provider (like Google). +admin.auth().updateUser(uid, { + providerToLink: { + uid: 'google_uid12345', + providerId: 'google.com' + } + }) + .then(function(userRecord) { + // See the UserRecord reference doc for the contents of userRecord. + console.log('Successfully updated user', userRecord.toJSON()); + }) + .catch(function(error) { + console.log('Error updating user:', error); + }); +// [END update_user_link_federated] + +// [START update_user_unlink_federated] +// Unlink the user from a federated identity provider (like Google). +admin.auth().updateUser(uid, { + providersToDelete: ['google.com'] + }) + .then(function(userRecord) { + // See the UserRecord reference doc for the contents of userRecord. + console.log('Successfully updated user', userRecord.toJSON()); + }) + .catch(function(error) { + console.log('Error updating user:', error); + }); +// [END update_user_unlink_federated] + // [START delete_user] admin.auth().deleteUser(uid) .then(function() { diff --git a/auth/tenant_management.js b/auth/tenant_management.js new file mode 100644 index 00000000..befab0ed --- /dev/null +++ b/auth/tenant_management.js @@ -0,0 +1,17 @@ +const admin = require('firebase-admin'); +admin.initializeApp(); + +function enableAnonymousSignIn() { + // [START auth_tenant_enable_anon] + const manager = admin.auth().tenantManager(); + manager.updateTenant('tenantId', { + anonymousSignInEnabled: true, + }) + .then(function(tenant) { + console.log('Successfully updated tenant: ', JSON.stringify(tenant)); + }) + .catch(function(error) { + console.log('Error updating tenant: ', JSON.stringify(error)); + }); + // [END auth_tenant_enable_anon] +} \ No newline at end of file From f6b7fb225df985a370723d2c1374901b97e6c596 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 31 Mar 2020 14:02:11 -0400 Subject: [PATCH 2/4] Review comments --- auth/manage_users.js | 4 +- auth/tenant_management.js | 82 ++++++++++++++++++++++++++++++++++----- 2 files changed, 75 insertions(+), 11 deletions(-) diff --git a/auth/manage_users.js b/auth/manage_users.js index 3d3ec0f0..bc755f33 100644 --- a/auth/manage_users.js +++ b/auth/manage_users.js @@ -126,8 +126,8 @@ admin.auth().updateUser(uid, { // Link the user with a federated identity provider (like Google). admin.auth().updateUser(uid, { providerToLink: { - uid: 'google_uid12345', - providerId: 'google.com' + providerId: 'google.com', + uid: 'google_uid12345' } }) .then(function(userRecord) { diff --git a/auth/tenant_management.js b/auth/tenant_management.js index befab0ed..6a931af5 100644 --- a/auth/tenant_management.js +++ b/auth/tenant_management.js @@ -1,17 +1,81 @@ const admin = require('firebase-admin'); admin.initializeApp(); +const tenantId = 'tenantId'; + +function createTenant() { + // [START auth_create_tenant] + admin.auth().tenantManager().createTenant({ + displayName: 'myTenant1', + emailSignInConfig: { + enabled: true, + passwordRequired: false, // Email link sign-in enabled. + }, + }) + .then((createdTenant) => { + console.log(createdTenant.toJSON()); + }) + .catch((error) => { + // Handle error. + }); + // [END auth_create_tenant] +} + +function updateTenant() { + // [START auth_update_tenant] + admin.auth().tenantManager().updateTenant(tenantId, { + displayName: 'updatedName', + emailSignInConfig: { + enabled: false, // Disable email provider. + }, + }) + .then((updatedTenant) => { + console.log(updatedTenant.toJSON()); + }) + .catch((error) => { + // Handle error. + }); + // [END auth_update_tenant] +} + +function deleteTenant() { + // [START auth_delete_tenant] + admin.auth().tenantManager().deleteTenant(tenantId) + .then(() => { + // Tenant deleted. + }) + .catch((error) => { + // Handle error. + }); + // [END auth_delete_tenant] +} + +// [START auth_list_all_tentants] +function listAllTenants(nextPageToken) { + return admin.auth().tenantManager().listTenants(100, nextPageToken) + .then((result) => { + result.tenants.forEach((tenant) => { + console.log(tenant.toJSON()); + }); + if (result.pageToken) { + return listAllTenants(result.pageToken); + } + }); +} +// [END auth_list_all_tentants] + + function enableAnonymousSignIn() { // [START auth_tenant_enable_anon] const manager = admin.auth().tenantManager(); - manager.updateTenant('tenantId', { - anonymousSignInEnabled: true, - }) - .then(function(tenant) { - console.log('Successfully updated tenant: ', JSON.stringify(tenant)); - }) - .catch(function(error) { - console.log('Error updating tenant: ', JSON.stringify(error)); - }); + manager.updateTenant(tenantId, { + anonymousSignInEnabled: true, + }) + .then(function(tenant) { + console.log('Successfully updated tenant: ', JSON.stringify(tenant)); + }) + .catch(function(error) { + console.log('Error updating tenant: ', JSON.stringify(error)); + }); // [END auth_tenant_enable_anon] } \ No newline at end of file From 8bd6944858c25503eebba6ed6164e5cafa052c13 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 31 Mar 2020 15:29:20 -0400 Subject: [PATCH 3/4] Combine snippets --- auth/tenant_management.js | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/auth/tenant_management.js b/auth/tenant_management.js index 6a931af5..9d662878 100644 --- a/auth/tenant_management.js +++ b/auth/tenant_management.js @@ -9,7 +9,8 @@ function createTenant() { displayName: 'myTenant1', emailSignInConfig: { enabled: true, - passwordRequired: false, // Email link sign-in enabled. + // Email link sign-in enabled. + passwordRequired: false, }, }) .then((createdTenant) => { @@ -26,8 +27,11 @@ function updateTenant() { admin.auth().tenantManager().updateTenant(tenantId, { displayName: 'updatedName', emailSignInConfig: { - enabled: false, // Disable email provider. + // Disable email provider. + enabled: false, }, + // Enable anonymous sign-in + anonymousSignInEnabled: true, }) .then((updatedTenant) => { console.log(updatedTenant.toJSON()); @@ -62,20 +66,4 @@ function listAllTenants(nextPageToken) { } }); } -// [END auth_list_all_tentants] - - -function enableAnonymousSignIn() { - // [START auth_tenant_enable_anon] - const manager = admin.auth().tenantManager(); - manager.updateTenant(tenantId, { - anonymousSignInEnabled: true, - }) - .then(function(tenant) { - console.log('Successfully updated tenant: ', JSON.stringify(tenant)); - }) - .catch(function(error) { - console.log('Error updating tenant: ', JSON.stringify(error)); - }); - // [END auth_tenant_enable_anon] -} \ No newline at end of file +// [END auth_list_all_tentants] \ No newline at end of file From 9dca306f8179f5e5b14eba692f9ef6cfac9aacfc Mon Sep 17 00:00:00 2001 From: Sam Stern Date: Thu, 2 Apr 2020 15:40:03 -0400 Subject: [PATCH 4/4] Update tenant_management.js --- auth/tenant_management.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/auth/tenant_management.js b/auth/tenant_management.js index 9d662878..a3d3858e 100644 --- a/auth/tenant_management.js +++ b/auth/tenant_management.js @@ -54,7 +54,7 @@ function deleteTenant() { // [END auth_delete_tenant] } -// [START auth_list_all_tentants] +// [START auth_list_all_tenants] function listAllTenants(nextPageToken) { return admin.auth().tenantManager().listTenants(100, nextPageToken) .then((result) => { @@ -66,4 +66,6 @@ function listAllTenants(nextPageToken) { } }); } -// [END auth_list_all_tentants] \ No newline at end of file + +listAllTenants(); +// [END auth_list_all_tenants]