登录用户名:Firebase,Swift [英] Sign In by UserName : Firebase,Swift
问题描述
我想让用户使用电子邮件登录,并使用用户名
是否可以使用Firebase?
<完全将您的用户帐户与任何特定于域的身份验证凭据分开,可能会在以后(绝对)真正的痛苦: -
$ b $ ul
更好的方法是用 email-ID
和 唯一的用户名
检查用户名是否已经存在:Firebase,Swift ,然后让用户选择使用用户名或电子邮件地址登录 。
将另一个节点添加到JSON树: -
$ p $ { ...
usernameEmailLink:{
uniqueUserName1:emailID1,$ b $ uniqueEserName2:emailID2,
uniqueUserName3:emailID3
}
...}
更新这个节点的安全规则:
{.rules:{
.read:auth!= null,
.write:auth!= null,
usernameEmailLink:{
.read:true,
.write:auth!= null
}
}
$ b $ p
$ pre> func retrieveUserEmail(userName:String,completionBlock:((userEmail:String) - > Void)){
var userEmail:String!
FIRDatabase.database()。reference()。child(usernameEmailLink / \(userName))。 b
$ b userEmail = snap.value!//这将给你用户的电子邮件ID
$ b $ * b $ b completionBlock(snap.value!)// A甚至更好的方式,如果这是所有这个功能。
* /
})
print(userEmail)
}
保存键值对: -
FIRDatabase.database()。reference()。child(usernameEmailLink)。updateChildValues [\(userName):\(Email)]
pre>
PS: - 但在此之前请确保您的用户名是唯一的。
I want to let user sign in with Email and also with User Name
Is that possible using Firebase ?
解决方案 Completely separating your users account from any kind of domain specific auth credential might be a real pain later(Most definitely):-
- Updating your user password
- Privacy
A better approach is to sign up your user with an email-ID
and a unique userName
Check if userName Already exists :Firebase, Swift, Then give your user option to Log In with a username or emailID.
Add another node to your JSON tree:-
{...
usernameEmailLink:{
uniqueUserName1:emailID1,
uniqueUserName2:emailID2,
uniqueUserName3:emailID3
}
...}
Update your security rules for this node:-
{".rules":{
".read" : "auth != null",
".write" : "auth != null",
"usernameEmailLink" :{
".read" : "true",
".write" : "auth != null"
}
}
}
Code:-
func retrieveUserEmail(userName : String, completionBlock : ((userEmail : String) -> Void)){
var userEmail : String!
FIRDatabase.database().reference().child("usernameEmailLink/\(userName)").observeSingleEvent(of: .value, with: {(snap) in
userEmail = snap.value! //This will give you the email ID for the user.
/*
completionBlock(snap.value!) // A even better way if this is all this function does.
*/
})
print(userEmail)
}
For saving the key-value pair:-
FIRDatabase.database().reference().child("usernameEmailLink").updateChildValues["\(userName)" : "\(Email)"]
PS:- But before this make sure your username is unique.
这篇关于登录用户名:Firebase,Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!