Skip to content
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

Snippets for new Auth admin features #94

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
42 changes: 42 additions & 0 deletions auth/manage_users.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ admin.auth().getUserByPhoneNumber(phoneNumber)
});
// [END get_user_by_phone]

// [START get_user_by_federated_id]
samtstern marked this conversation as resolved.
Show resolved Hide resolved
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' },
Expand Down Expand Up @@ -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: {
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().deleteUser(uid)
.then(function() {
Expand Down
81 changes: 81 additions & 0 deletions auth/tenant_management.js
Original file line number Diff line number Diff line change
@@ -0,0 +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.
},
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.)

})
.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]
samtstern marked this conversation as resolved.
Show resolved Hide resolved
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);
}
});
}
samtstern marked this conversation as resolved.
Show resolved Hide resolved
// [END auth_list_all_tentants]


function enableAnonymousSignIn() {
// [START auth_tenant_enable_anon]
samtstern marked this conversation as resolved.
Show resolved Hide resolved
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]
}