I want to let user sign in with Email and also with User Name Is that possible using Firebase ?
Asked
Active
Viewed 3,040 times
1
-
Yes it is, you can find these informations here in the firebase docs well explained https://firebase.google.com/docs/auth/ios/password-auth and a good tutorial for showing you step by step how to do it https://www.raywenderlich.com/139322/firebase-tutorial-getting-started-2 – ronatory Oct 18 '16 at 06:40
-
@ronatory Thank you , but I had road these it let you sign in via Email only ,I want sign in with "User Name" – Basel Oct 18 '16 at 07:10
-
Ok then this answer might help you http://stackoverflow.com/a/35121112/5327882 . Please do a little bit more research before asking these kind of questions – ronatory Oct 18 '16 at 07:27
-
@ronatory This isn't a good solution because it let user enter username and then save it as fake email for example user@appname.com in this solution the user can only sign in with username without his real email , and The user can't reset his password because it doesn't store in the database as real email , what I want is make him sign with "Email" and also with "User Name" , I Don't find any similar question That why I ask it. – Basel Oct 18 '16 at 12:32
1 Answers
3
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 userNameCheck 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.
-
How can I set both values together like "uniqueUserName1:emailID1" ? and I got these error " Error saving rules - Line 6: Expected ',' or '}'. " – Basel Oct 18 '16 at 13:45
-
-
-
-
Last quotation how can I pass a value in snapshot ? what I mean I want to make Function retrieveUserEmail return snap.value! I don't know how , anything in side "})" I can't get them outside , I try to make variable outside FIRDatabase and then make snap.value! equal to it , but it doesn't work , always get nil – Basel Oct 18 '16 at 15:41
-
-
after i tried it many time , I noticed , every time I sign up with new account it deleted the first one , always I have one line of uniqueUserName1:emailID1 , i used the same line 'FIRDatabase.database().reference().child("usernameEmailLink").setValue["\(userName)" : "\(Email)"]' , I don't know where is the problem , could you help me ? – Basel Oct 19 '16 at 06:16
-
Answer updated :) Anyways avoid extending your queries in one question, ask a new question on SO. – Dravidian Oct 19 '16 at 06:20
-
Thank you very very much , That was helpful , Ok I will , Have a nice day – Basel Oct 19 '16 at 06:48
-
1It should be noted that this makes users' email addresses publicly accessible. – Jacob Phillips Feb 22 '17 at 05:29
-
where does the saving for the key value going ? I'm really strugling to make this work, if anybody can help me out ? it will be incredible !! here is my code if it can help . .. . https://stackoverflow.com/questions/44111403/sign-in-with-username-instead-of-email-into-ios-app-and-firebase thanks a lot ! – tibewww May 26 '17 at 10:33