用户在以前注销后错误地使用无效数据登录

时间:2015-09-04 14:59:16

标签: ios swift parse-platform login uistoryboardsegue

我有一个登录屏幕,可确保在尝试登录之前输入字段中有有效数据。或者我想。

问题是当我们从另一个屏幕返回"注销"用户,如果我在返回此页面后使用无效的用户名密码组合提交,我会按预期看到错误对话框,但在解除后,我会被带到下一个视图控制器,就像我登录一样。

请帮忙吗?

@IBAction func btnSubmit(sender: UIButton) {
        if txtUsername.text == "" || txtPassword.text == "" {
            //they're missing a username or password
            displayAlert("Missing Field(s)", message: "Please enter both a username and password")
        }else {
            //we check if they're in signup/login mode
            if Switch.on {
                //user is in signup mode
                if txtPassword.text != txtConfirmPassword.text {
                    //the password fields do not match
                    displayAlert("Mismatched Passwords", message: "Please enter matching passwords")
                }else {
                    //the password fields do match, and the user can register with this username/email and password
                    var user = PFUser()
                    user.username = txtUsername.text
                    user.password = txtPassword.text
                    // other fields can be set just like with PFObject

                    user.signUpInBackgroundWithBlock {
                        (succeeded: Bool, error: NSError?) -> Void in
                        if let error = error {
                            let errorString = error.userInfo?["error"] as! String
                            // Show the errorString somewhere and let the user try again.
                            self.displayAlert("Signup Error", message: errorString)
                        } else {
                            // Hooray! Let them use the app now.
                            self.performSegueWithIdentifier("register", sender: self)
                        }
                    }                }
            }else {
                //user is in login mode and we can submit credentials


                PFUser.logInWithUsernameInBackground(txtUsername.text, password:txtPassword.text) {
                    (user: PFUser?, error: NSError?) -> Void in
                    if let error = error {
                        let errorString = error.userInfo?["error"] as! String
                        // Show the errorString somewhere and let the user try again.
                        self.displayAlert("Login Error", message: errorString)
                    } else {
                        if PFUser.currentUser()!.username != nil {
                            // Do stuff after successful login.
                            self.performSegueWithIdentifier("login", sender: self)
                        }
                    }

                }
            }
        }
    }

这是我从其他页面注销的

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "logout" {
            PFUser.logOut()

        }
    }

1 个答案:

答案 0 :(得分:0)

我认为问题在于你没有使用在完成处理程序中返回的成功的Bool。当我使用Parse登录用户时,主要使用它来查看登录是否成功,如果不成功,我将检查错误消息是什么。这可以阻止您在用户未正确登录时允许用户继续使用该应用程序。

user.signUpInBackgroundWithBlock {
    (succeeded: Bool, error: NSError?) -> Void in
    if error != nil {
        let errorString = error!.userInfo?["error"] as! String
        // Show the errorString somewhere and let the user try again.
        self.displayAlert("Signup Error", message: errorString)
    } else {
        if succeeded {
            // Hooray! Let them use the app now.
            self.performSegueWithIdentifier("register", sender: self)
        } else {
            //Something went wrong
        }
    }
}