Skip to content

Snippets for new Auth admin features #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions auth/manage_users.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ admin
});
// [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()
Expand Down Expand Up @@ -132,6 +143,37 @@ admin
});
// [END update_user]

// [START update_user_link_federated]
// Link the user with a federated identity provider (like Google).
admin.auth().updateUser(uid, {
providerToLink: {
providerId: 'google.com',
uid: 'google_uid12345'
}
})
.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()
Expand Down
71 changes: 71 additions & 0 deletions auth/tenant_management.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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,
// Email link sign-in enabled.
passwordRequired: false,
},
})
.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: {
// Disable email provider.
enabled: false,
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally, I had thought to just piggy-back the enablement of anon authentication docs into the existing create/update tenant docs. My reasoning was that this is just another parameter, not really much different than (eg) emailSignInConfig. If you did that here, you could get rid of the enableAnonAuth snippet entirely. (No objection to keeping it separate either though.)

// Enable anonymous sign-in
anonymousSignInEnabled: true,
})
.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_tenants]
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);
}
});
}

listAllTenants();
// [END auth_list_all_tenants]