首页 > 解决方案 > 如何检查用户是否在 firebase swift 应用程序中正确登录?

问题描述

我有问题,即使我输入了错误的密码,我的应用程序也会向我显示有关成功登录的信息,但实际上我没有。如何使其正常工作?

Auth.auth().fetchSignInMethods(forEmail: userEmail, completion: {
            (providers, error) in

            if error != nil {
                self.displayAlertMessage(alertTitle: "Unhandled error", alertMessage: "Undefined error #SignUpViewController_0001");
                return;
            } else if providers == nil {
                self.displayAlertMessage(alertTitle: "Error", alertMessage: "This account is not exist.");
                return;
            }
        })

        // Login

        Auth.auth().signIn(withEmail: userEmail, password: userPassword) { [weak self] authResult, error in
            guard self != nil else {
                self?.displayAlertMessage(alertTitle: "Alert", alertMessage: "Wrong password.");
                return }
        }

        self.displayAlertMessage(alertTitle: "Success", alertMessage: "You are successfuly sign in.", dismiss: true);

        // Return to initial view

标签: iosswiftfirebasefirebase-authentication

解决方案


Auth.auth().signIn()是异步的并立即返回。稍后将使用登录结果调用您传递的回调。您的代码所做的是self.displayAlertMessage(alertTitle: "Success",...)在登录完成之前立即调用。您应该只期望回调内的登录结果,而不是调用后的下一行代码signIn()


推荐阅读