Sign in with username instead of email into ios app and firebase

天大地大妈咪最大 提交于 2019-12-25 05:05:18

问题


Here is my situation, I am using firebase as a DB for an iOS App.

user are able to login via their Email by:

    @IBAction func signInButtonPressed(_ sender: UIButton)

    {
    if let email = emailField.text, let password = passwordField.text
    {
        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 all working perfectly.

However, I would like the user to login with their username instead of their email.

I create user in the AUth and the data are also copied into the DB with taht structure:

--name of db
----posts
----users
------username
------email
------id

How is that possible to do ?

Thanks a lot for all your help !

----- EDIT -----

By adding this:` // START CHECK

@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 can get the snap in my console when entering the correct username, which display:

Snap (username written) {
    username = username;
    email = "email@domain.net";
    id = 8mbwfwMW0kZ4ip5mhyvEySxm4iI2;
}

How do i do to get the email value so ?

I'm struggling quite a lot with all this, any help will be amazing !

THanks for your help and time !!

来源:https://stackoverflow.com/questions/44111403/sign-in-with-username-instead-of-email-into-ios-app-and-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!