简体   繁体   中英

Parse email and username login

I'm trying to login a user by using the username or email. Right now my code works to log the user in by only Username. I'm using a "PFUser.logInWithUsername", but I'd also like to login with the users email. I'm trying to change my code to allow the user to have the choice to either email or a username. Here is my code.

@IBAction func LogInButton(_ sender: AnyObject) {

    // login functions
    PFUser.logInWithUsername(inBackground: UsernameOrEmail.text!, password: Password.text!) { (user:PFUser?, error:Error?) -> Void in
        if error == nil {

            // remember user or save in App Memeory did the user login or not

            UserDefaults.standard.set(user!.username, forKey: "username")
            UserDefaults.standard.synchronize()

            // call login function from AppDelegate.swift class
            let appDelegate : AppDelegate = UIApplication.shared.delegate as! AppDelegate

            // Delay the dismissal by 5 seconds
            let delay = 1.0 * Double(NSEC_PER_SEC)
            var time = DispatchTime.now() + Double(Int64(delay)) / Double(NSEC_PER_SEC)
            DispatchQueue.main.asyncAfter(deadline: time, execute: {

                appDelegate.login()

            })

        } else {
     }

You can use a regex pattern to detect if the user enters an email. If the detection returns false, then you "know" that the user entered their username.

This is what I use in my applications(works ok):

//A function that returns true or false based on the input. True if email, false if something else.
func isValidEmail(email:String) -> Bool {
    let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
    let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
    return emailTest.evaluate(with: email)
}


//check if user enters email or not:
if isValidEmail(email: user!.username){
        //email adress detected
    }else{
        //username detected
    }

EDIT:

As I understand your problem the function above will solve your problem. I have provided a some code for you to test out. I do not know what PFUser is capable of, but I assume there is a function for login with username and another for email.

//A function that returns true or false based on the input. True if email, false if something else.
func isValidEmail(email:String) -> Bool {
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
return emailTest.evaluate(with: email)
}


@IBAction func LogInButton(_ sender: AnyObject) {

//check if user enters username or email

    if let usercredentials = UsernameOrEmail.text {     //get the username from textfield

        if isValidEmail(email: usercredentials){
            //user did enter his email as login credential
            PFUser.logInWithEmail(inBackground: usercredentials, password: Password.text!) { (user:PFUser?, error:Error?) -> Void in
            if error == nil {
                //do your login stuff here
            } else {

        }
        }else{
            //user did enter his username as login credential
            PFUser.logInWithUsername(inBackground: usercredentials, password: Password.text!) { (user:PFUser?, error:Error?) -> Void in
                if error == nil {
                    //do your login stuff here
                } else {
            }
    }else{
    //textfield not accessible
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM