0

I am new to Swift and Firebase. I am developing an IOS application. For this App I need authenticate user email and password. I have a Json file with user details. Please help me how to authenticate user email and password?I have read several posts in Stackoverflow, but I am stuck. Following are my Json and swift code.

Json file:

{
   "ID": 123,
   "Membership": 1234,
   "title": "Mr.",
   "firstname": "kumar",
   "lastname": "sandeep",
   "email": "sandeepcs2005@gmail.com",
   "membrshipstatus": "Active",
   "volunteer": "Yes",
   "creationDate": "2016-12-27 "
 },

This is my swift code:

import UIKit
import Firebase
import FirebaseAuth



class ViewController: UIViewController {

    @IBOutlet weak var emailofUser: UITextField!
    @IBOutlet weak var passwordofUser: UITextField!
    var ref:FIRDatabaseReference! //created a variable ref of type firebase database reference
    var databaseHandle:FIRDatabaseHandle? //to handle to database listener like to stop or start it



    var postdata = [String]?()
    var postall = [[String:String]]()



    override func viewDidLoad() {
        super.viewDidLoad()



            //set firebase reference
        ref = FIRDatabase.database().reference()

        let userRef = ref.child("Hub")
        //let queryRef = userRef.queryOrderedByChild("email").queryEqualToValue("mr.stefankirsch@gmx.com")


        // Consider adding ".indexOn": "email" at /hubeindhoven-95f09 to your security rules for better performance


       userRef.queryOrderedByChild("email").queryEqualToValue("mr.sandeep@gmail.com").observeEventType(.Value, withBlock: { snapshot in

            for child in snapshot.children {

            let snap = child as! FIRDataSnapshot

                let userDict = snap.value as! [String:Any]

                let userId = userDict["ID"]
                let lastname = userDict["lastname"]
                print("\(userId!)  \(lastname!)")


            }
                    })
  • Look this: http://stackoverflow.com/a/40762653/3108877 – Rob Feb 15 '17 at 14:42
  • Hi Rob, thank you. But that does not answer my question. I already have users in JSON file and I have to validate them. How can i do this? Please help. – Sandeep Kumat Feb 15 '17 at 15:14

2 Answers2

1

I think your firebase json what you have posted is wrong, are you following standard way to add user (in firebase web interface). There is no unique id for each user. For example I have added one of mine..

{
  "users" : {
    “UOLTzymgIyeGgNBSS0UgUs95od21” : {
      "email" : "aaaaa@gmail.com",
      "firstName" : "aaaaa",
      "lastName" : “bbbb”,
      "mobileNo" : 12122121
    },
    “CveQ13sLmMO93yHZEAP24xHA3VZ1” : {
      "email" : "bbbb@gmail.com",
      "firstName" : “bbbb”,
      "lastName" : “bbbbb”,
      "mobileNo" : 11223333
    }
  }
}

I have added code for signin, this worked for me, hope this will solve your problem

    override func viewDidLoad() {
        super.viewDidLoad()

        FIRAuth.auth()!.addStateDidChangeListener() { auth, user in
            if user != nil {
               self.performSegueWithIdentifier("UserDetails", sender: self)
            }
        }
   }

@IBAction func LoginPage(sender: UIButton) {
        FIRAuth.auth()!.signIn(withEmail: textFieldLoginEmail.text!,
                               password: textFieldLoginPassword.text!)
}
Ratnesh Shukla
  • 1,128
  • 13
  • 24
1

There are a few issues that need to be addressed.

The first one is that Firebase handles email/password authentication for you, and once a Firebase user is created, authenticating is very simple:

FIRAuth.auth()?.signIn(withEmail: email, password: password) { (user, error) in
  // ...
}

An additional JSON structure in Firebase is not required. However, if you want to store additional information about each user, levering the users user id (uid) is the way to go. That uid is auto generated with when creating the user in firebase and then you can also create a separate node to contain other user info

users
   uid_0
     name: "Leeeeroy"
     favorite_game: "WoW"

Seeing as you already have users in a JSON structure, I would recommend crafting up a quick app to iterate over those users, creating their Firebase account and adding personal data to a users node.

Creating users is also simple

FIRAuth.auth()?.createUser(withEmail: email, password: password) { (user, error) in
  // ...
}

If you really don't want to create Firebase users, you could roll your own authentication scheme (not recommended however).

Jay
  • 34,438
  • 18
  • 52
  • 81