Categories
ios swift

User Signup and Login with Firebase for IOS

With Parse being killed off by facebook I wanted to give Firebase a shot. Here is a simple Signup and Login example using Swift in iOS.

First, you want to go ahead and create an account. They offer a free development account, this should be enough to get you going.

This article is a big help in getting Firebase integrated into your Xcode project. https://www.firebase.com/docs/ios/quickstart.html

Below is commented code of the example.

//
//  ViewController.swift
//  firebaseLoginExample
//

import UIKit
import Firebase // 1. import Firebase

class ViewController: UIViewController {
    
    var fireBaseRef = Firebase(url: "https://hazrrd.firebaseio.com/users") // 2. Reference your applications firebase URL
    
    
    @IBOutlet weak var email: UITextField! // Setup an outlet to a email textfield
    @IBOutlet weak var password: UITextField! // Setup an outlet to a password textfield
    
    var authUID: String?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // We setup an observer to let us know the status of the user helps to know if they are logged in or not. The AuthID is key.
        fireBaseRef.observeAuthEventWithBlock { (authData) -> Void in
            if authData != nil {
                print(authData)
            }
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

// This action is tied to a button
    @IBAction func signUp(sender: AnyObject) {
        
        guard self.email.text != "" else {
It is almost impossible to predict professional cialis appalachianmagazine.com a sexual disorder. Sodium is known to raise blood pressure, even in those that don't have high blood pressure is more likely to experience erection issues.  levitra 20mg uk Try to avoid fatty meals when taking this drug you  cialis wholesale india may suffer from few side effects, such as stomach cramps, diarrhea, vomiting, nausea. While oral medication improves viagra generic discount  the reaction to sexual stimulation, they will not exhibit their effect.             print("Username required")
            return
        }
        
        guard self.password.text != "" else {
            print("Password required")
            return
        }
        
        
        
        // 4. Call the createUser method which takes two arguments (email, password) both of type String
       fireBaseRef.createUser(self.email.text, password: self.password.text) { (error, result) -> Void in
            if error != nil {
                print(error) // print if an error is present
            } else {
                let userID = result["uid"] as? String // If the new user is created successfully the callback will supply a UID that is unique to the user
                print("Successfully created a new user with UID \(userID!)")
                
                // Now, creating a user does not login them in automatically, which is what we will do next. 
                // Here we will call the authUser method and pass (email, password) as we did before.
                self.fireBaseRef.authUser(self.email.text, password: self.password.text, withCompletionBlock: { (error, authData) -> Void in
                    if error != nil{
                        print(error) // print if an error is present
                    } else {
                        print("Successfully logged in new user width UID \(userID!)")
                    }
                })
                
            }
        }
   
    }
    

  
}

Here is a screenshot of a newly added user in the Firebase dashboard.

Screen Shot 2016-02-04 at 2.33.13 PM