[英]Check if user is already signed in to Firebase Authentication by phone number
[英]Firebase - Predefined User list & Phone Number Authentication
我正在開發一個 Android 應用程序並希望將其與 Firebase 及其實時數據庫集成。 我有一個包含 1000 個用戶 (Excel) 的列表,其中包含 EmpId、EmpName、團隊、手機號碼等詳細信息。
我想將我的應用程序限制為僅供此列表中的用戶使用,並且還想使用列表中顯示的手機號碼對他們的姓名進行身份驗證。
我可以使用 Firebase Auth 滿足上述要求嗎?如果可以,該怎么做?
如果使用 FireBase Auth,這是不可能的替代解決方案是什么?
請幫忙。
Firebase 身份驗證僅允許用戶識別自己。 您所描述的是限制允許哪些用戶使用您的應用程序,這稱為授權,而 Firebase 身份驗證無法處理該問題。
幸運的是,您也使用firebase-realtime-database
了標記,並且其中肯定內置了授權。 我通常會在數據庫中創建一個頂級節點,其中包含允許使用該應用程序的用戶的 UID:
"allowedUsers": {
"uidOfUser1": true,
"uidOfUser2": true
...
}
然后在其他安全規則中,您將在允許用戶訪問數據之前檢查用戶的 UID 是否在此列表中,例如:
{
"rules": {
"employees": {
".read": "root.child('allowedUsers').child(auth.uid).exists()",
"$uid": {
".read": "auth.uid === $uid && root.child('allowedUsers').child(auth.uid).exists()"
}
}
}
}
使用這些規則:
當然,您會希望修改這些規則以適應您自己的要求,但希望這能讓您開始。
有幾點要記住:
allowedUsers
列表。.
不允許使用字符,因此您必須對其進行某種形式的編碼。另見:
我知道現在已經晚了,但對於任何可能提到這個問題的人,我的建議是blocking functions 。 您實質上創建了一個 Firebase Cloud Function,它可以接受或拒絕創建帳戶的請求。 它可能是這樣的:
const functions = require('firebase-functions');
exports.beforeCreate = functions.auth.user().beforeCreate((user, context) => {
var allowedUsers = ['johndoe@stackoverflow.com'];
// if they are not in the list, they cannot make an account
// throwing this error will prevent account creation
if(allowUsers.indexOf(user.email) == -1) throw new functions.auth.HttpsError('permission-denied');
});
我認為這種方式更好,因為安全規則不必引用數據庫。 相反,安全規則可以允許來自任何經過身份驗證的用戶的請求,因為唯一經過身份驗證的用戶是此 function 允許的用戶。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.