here is my situation for my iOS app ( I am using swift 3.0 and firebase).
I have a login screen, where the user are able to login via their E-mail using the Auth of firebase. For that I am using this code:
@IBAction func signInButtonPressed(_ sender: UIButton)
{
if let email = emailField.text, let password = passwordField.text
{
// FIRDatabase.database().reference().child("users").child(self.emailField.text!).observeSingleEvent(of: .value, with: {(userEmail) in
// FIRAuth.auth()?.signIn(withEmail: email, password: password)
FIRAuth.auth()?.signIn(withEmail: email, password: password)
{
(user, error) in
if let error = error
{
let alertController = UIAlertController(title: "Login or password incorrect", message:
"", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default,handler: nil))
self.present(alertController, animated: true, completion: nil) }
else
{
print("AUTH: EMAIL AUTH SUCCESSFUL")
User.currentUserId = user?.uid
User.startTrackingCurrentUser()
self.performSegue(withIdentifier: "ToFeed", sender: nil)
}
}
}
}
Which is working perfectly !
What I'am trying to achieve, is instead to sign in user via their username. For that I believe the best thing is when recognising if the user exist, to send the email value instead - fetching the data from the DB.
My DB is configure correctly, and fetch the auth system such as:
--name of db
----posts
----users
------username
------email
------id
And I get the user recognise in my console when typing the correct username in the text field via:
@IBAction func textFieldEditingDidChange(_ sender: Any) {
FIRDatabase.database().reference().child("users").child(self.emailField.text!).observeSingleEvent(of: .value, with: {(email) in
if let userDict = email.value as? [String:AnyObject]{
for each in userDict{
let email = each.1 as! String
}
}
if email.exists(){
print(email)
}else{
print("USER NOT EXIST")
}
})
}
I'm having display in my console the following when the username written is correct:
Snap (myuser) {
username = myuser;
email = "myemail@email.com";
id = 8mbwXUMe0Ye4ip2mhhvEySxmxiI5;
}
For this I have been following this thread:
Sign In by UserName : Firebase,Swift
From now, how can i on submit, use the email value from the snap instead of the username to be able to login ? I don't manage to make this work, I have been working on that for a week now . . .
Any help will be superb ! I'm starting on iOS Dev.
Thanks a lot !
---EDIT --
Now I'm managing to have only the email try print in my console via:
IBAction func textFieldEditingDidChange(_ sender: Any) {
FIRDatabase.database().reference().child("users").child(self.emailField.text!).observeSingleEvent(of: .value, with: {(email) in
if let userDict = email.value as? [String:AnyObject]{
print(email)
}
if email.exists(){
}else{
print("USER NOT EXIST")
}
})
}
But still looking how to pass the value ?