Firebase身份验证错误,但仍在进行身份验证并转到下一个视图

时间:2016-04-27 22:46:18

标签: ios firebase completionhandler

我正在创建一个与Firebase接口的iOS应用程序。我传递了一个虚拟用户ID和密码,但是当传入无效凭据时,应用程序仍然会遇到真正的完成处理程序(这使我相信它可能是一个简单的代码错误,因为我有限的Swift知识(我是还在学习。)

为什么完成处理程序仍然返回true?可以采取哪些措施来解决这个问题?

请参阅以下代码。

LoginViewController.swift

import UIKit

class LoginViewController: UIViewController {

    let database = Database.sharedInstance

    @IBOutlet weak var studyID: UITextField!
    @IBOutlet weak var userID: UITextField!
    @IBOutlet weak var signInButton: UIButton!


    @IBAction func signInPressed(sender: AnyObject) {

        if (!studyID.text!.isEmpty && !userID.text!.isEmpty){
            self.database.validate(userID.text!, study:studyID.text!, pass:userID.text!) {
                (value: Bool) in
                if value {
                    self.performSegueWithIdentifier("loggedIn", sender: nil)
                } else {
                    let errorController = UIAlertController(
                        title: "Error",
                        message: "There was an error with authentication.",
                        preferredStyle: .Alert)
                    // set an action button on the alert
                    let okAction = UIAlertAction(title: "OK", style: .Default, handler: {
                        (errorController: UIAlertAction!) -> Void in
                    })
                    // add the action
                    errorController.addAction(okAction)
                    // show the alert to the user with animation

                    self.presentViewController(errorController, animated: true, completion: nil)

                }
            }
        } else {
            let blankController = UIAlertController(
                title: "Error",
                message: "Please fill in all fields.",
                preferredStyle: .Alert)
            // set an action button on the alert
            let okAction = UIAlertAction(title: "OK", style: .Default, handler: {
                (blankController: UIAlertAction!) -> Void in
            })
            // add the action
            blankController.addAction(okAction)
            // show the alert to the user with animation

            self.presentViewController(blankController, animated: true, completion: nil)
        }
    }


}

DatabaseHandler.swift

import Foundation
import Firebase

class Database {

    var firebaseRef = Firebase(url:"https://<UNIQUE>.firebaseio.com")

    class var sharedInstance: Database {
        struct Data {
            static var instance: Database?
            static var token: dispatch_once_t = 0
        }

        dispatch_once(&Data.token) {
            Data.instance = Database()
        }

        return Data.instance!
    }

    var uid : String!

    var Question1 : String!
    var Question2 : String!
    var Question3 : String!
    var Question4 : String!
    var Score : Int!

    var Question1 : String!
    var Question2 : String!
    var Question3 : String!
    var Question4 : String!
    var Score : Int!


    var validated = false

    func validate(user: String, study: String, pass: String, completionHandler:(result: Bool) -> Void) {

        firebaseRef.authUser(user+"@email.com", password: pass,
                             withCompletionBlock: { error, authData in
                                if error != nil {
                                    completionHandler(result: false)
                                    return
                                } else {
                                    self.uid = authData.uid
                                    NSLog(authData.uid)
                                }
        })

        let usersRef = Firebase(url: "https://<UNIQUE>.firebaseio.com/users")
        usersRef.observeEventType(FEventType.Value, withBlock: { (snapshot) in
            if let value = snapshot.value.objectForKey("study") as? String { 
                if value == study {
                    completionHandler(result: true)
                    return
                } else {
                    completionHandler(result: false)
                    return
                }
            } else {
                completionHandler(result: false)
                return
            }
        })
    }

    func sendQuestionData(type: String) {
        let ref = Firebase(url:  "https://<UNIQUE>.firebaseio.com/users/" + uid + "/weeks/" + "1" + "/" + type + "/")
        var json = NSDictionary()
        if type == "Condition1" {
            json = ["Question1" : Question1, "Question2" : Question2, "Question3" : Question3,
                        "Question4" : Question4, "Score" : Score]
        } else {
            json = ["Question1" : Question1, "Question2" : Question2, "Question3" : Question3,
                        "Question4" : Question4, "Score" : Score]
        }
        NSLog(String(json))
        ref.setValue(json)
    }

}

0 个答案:

没有答案
相关问题