本文檔向您展示如何使用 Firebase Admin SDK 以程式設計方式管理多因素使用者。管理多因素使用者時,與單因素使用者相比,您可以存取更多的使用者屬性。
在你開始之前
安裝 Node.js 管理 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);
});