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": "[email protected]", "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("[email protected]") // Consider adding ".indexOn": "email" at /hubeindhoven-95f09 to your security rules for better performance userRef.queryOrderedByChild("email").queryEqualToValue("[email protected]").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!)") } }) 
2
  • Look this: stackoverflow.com/a/40762653/3108877 Commented Feb 15, 2017 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. Commented Feb 15, 2017 at 15:14

2 Answers 2

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" : "[email protected]", "firstName" : "aaaaa", "lastName" : “bbbb”, "mobileNo" : 12122121 }, “CveQ13sLmMO93yHZEAP24xHA3VZ1” : { "email" : "[email protected]", "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!) } 
Sign up to request clarification or add additional context in comments.

10 Comments

Hi @Jay, Thank you for the wonderful reply. In my situation, users are already created and the data is stored in Easy dus database. So now I have to export the user details (email, passoword, name, etc,,) in CSV and convert it to JSON. Then I have to import JSON file and authenticate users.
@Jay Now I need to import JSON file to the Database and I have to validate user login. How can I achieve this? Please help
Hi Ratnesh, thank you for the excellent reply. JSON structure I posted is an example, but soon I will post the required JSON file.
Looking at your Json file I got a doubt. Where did your JSON file located, in Database or ?
Hi all, I have changed the JSON file, that is the exact structure I am working.
|
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).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.