本文档介绍了如何使用 Firebase Admin SDK 以编程方式管理多重身份验证用户。与单一身份验证用户相比,管理多重身份验证用户时,您可以访问更多用户属性。
准备工作
安装 Node.js Admin SDK。目前不支持其他 Admin SDK 语言。
获取用户
您可以从 UserRecord
对象中检索与用户多重身份验证相关的数据,例如已注册的第二重身份验证列表。要获取用户记录,请调用 getUser()
或 getUserByEmail()
。
以下示例显示了一个注册了多重身份验证的用户:
// console.log(userRecord.toJSON());
{
uid: 'some-uid',
displayName: 'John Doe',
email: 'johndoe@gmail.com',
photoURL: 'http://www.example.com/12345678/photo.png',
emailVerified: true,
phoneNumber: '+11234567890',
// Set this user as admin.
customClaims: {admin: true},
// User with Google provider.
providerData: [{
uid: 'google-uid',
email: 'johndoe@gmail.com',
displayName: 'John Doe',
photoURL: 'http://www.example.com/12345678/photo.png',
providerId: 'google.com'
}],
multiFactor: {
enrolledFactors: [
// 2FA with SMS as 2nd factor.
{
uid: '53HG4HG45HG8G04GJ40J4G3J',
phoneNumber: '+16505551234',
displayName: 'Work phone',
enrollmentTime: 'Fri, 22 Sep 2017 01:49:58 GMT',
factorId: 'phone',
},
],
},
};
列出用户
以下代码显示了如何列出所有用户并查看他们是否注册了第二重身份验证:
admin.auth().listUsers(1000, nextPageToken)
.then((listUsersResult) => {
listUsersResult.users.forEach((userRecord) => {
// Multi-factor enrolled users second factors can be retrieved via:
if (userRecord.multiFactor) {
userRecord.multiFactor.enrolledFactors.forEach((enrolledFactor) => {
console.log(userRecord.uid, enrolledFactor.toJSON());
});
}
});
})
.catch((error) => {
console.log('Error listing users:', error);
});
用户会被批量返回,并按 uid
排序。每批结果都包含用户列表和用于提取下一批用户的下一页标志。列出所有用户后,系统不会返回 pageToken
。
maxResult
字段指定最大批次大小。默认值以及最大值为 1000。
创建用户
调用 createUser()
创建新用户。具有第二重身份验证的新用户必须拥有经过验证的电子邮件地址(将 emailVerified
设置为 true
),并使用受支持的第一重身份验证登录。每个用户最多可以有 5 个第二重身份验证。
以下示例显示了如何创建具有 2 个第二重身份验证的新用户:
admin.auth().createUser({
uid: '123456789',
email: 'user@example.com',
emailVerified: true,
password: 'password',
multiFactor: {
enrolledFactors: [
// When creating users with phone second factors, the uid and
// enrollmentTime should not be specified. These will be provisioned by
// the Auth server.
// Primary second factor.
{
phoneNumber: '+16505550001',
displayName: 'Corp phone',
factorId: 'phone',
},
// Backup second factor.
{
phoneNumber: '+16505550002',
displayName: 'Personal phone',
factorId: 'phone'
},
],
},
})
.then((userRecord) => {
console.log(userRecord.multiFactor.enrolledFactors);
})
.catch((error) => {
console.log(error);
});
更新用户
要更新现有用户,请调用 updateUser()
:
admin.auth().updateUser(uid: '123456789', {
multiFactor: {
enrolledFactors: [
{
// uid will be auto-generated.
phoneNumber: '+16505550003',
displayName: 'Spouse\'s phone',
factorId: 'phone',
},
{
// uid can also be specified. This is useful if a new second factor is added and an
// existing enrolled second factor is kept unmodified.
uid: 'existing-enrolled-mfa-uid',
phoneNumber: '+16505550004',
displayName: 'Personal phone',
factorId: 'phone',
},
{
phoneNumber: '+16505550005',
displayName: 'Backup phone',
factorId: 'phone',
// Enrollment time can also be explicitly specified.
enrollmentTime: new Date().toUTCString(),
},
],
},
})
.then((userRecord) => {
console.log(userRecord.multiFactor.enrolledFactors);
})
.catch((error) => {
console.log(error);
});
添加新的第二重身份验证
如果使用 enrolledFactors
列表调用 updateUser()
,则系统将清除用户当前的任何第二重身份验证。要在保留现有第二重身份验证的同时添加新的第二重身份验证,请先查找用户,然后将新的身份验证添加到列表中:
function enrollSecondFactor(userId, secondFactorPhoneNumber, secondFactorDisplayName) {
return admin.auth().getUser(userId)
.then((userRecord) => {
const updatedList = (userRecord.multiFactor &&
userRecord.multiFactor.toJSON().enrolledFactors) || [];
updatedList.push({
phoneNumber: secondFactorPhoneNumber,
displayName: secondFactorDisplayName,
factorId: 'phone',
});
return admin.auth().updateUser(userRecord.uid, {
multiFactor: {
enrolledFactors: updatedList,
},
});
})
.catch((error) => {
console.log(error);
});
}
移除第二重身份验证
如需完全取消用户的多重身份验证注册,请将 enrolledFactors
设置为 null
或空数组:
admin.auth().updateUser(uid: '123456789', {
multiFactor: {
enrolledFactors: null,
},
})
.then((userRecord) => {
console.log(userRecord.multiFactor);
})
.catch((error) => {
console.log(error);
});