Latest Posts

Sorry, no posts matched your criteria.

Stay in Touch With Us

Odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore.

Email
magazine@example.com

Phone
+32 458 623 874

Addresse
302 2nd St
Brooklyn, NY 11215, USA
40.674386 – 73.984783

Follow us on social

The Gadget Book

How To Build a Sign Up & Login Page with Swift & Firebase

In this Tutorial you will learn How To Build a Sign Up & Login page with Swift & Firebase.

So for this Tutorial we are going to be using the Firebase Authentication Pod, which allows developers to Authenticate users easily and securely. You need to jump into your Firebase Console and enable the Sign-In method to login with our app. In the Firebase Console, go to the Authentication tab and enable the Email/Password provider and hit Save.

Firebase Email / Password Login

Create a new Xcode project and add the Firebase SDK, our Tutorial will show you how. Make sure you add the Authentication Pod to you pod file, which authenticate users and allow them to login.

Firebase Auth Pod Install

In Xcode you want to create a layout for the Login & Sign up page, I added a Segmented Control , two Text Fields, for the email and password, and also a Button. You also want to Create Outlets for all of these elements to your swift file and also a Action for your Button.

@IBOutlet weak var seg: UISegmentedControl!
@IBOutlet weak var email: UITextField!
@IBOutlet weak var pass: UITextField!
@IBOutlet weak var btn: UIButton!
    
@IBAction func btn_clicked(_ sender: Any) {
        
}

Login & Sign up Page Swift Firebase

We want to import the Firebase SDK into our swift file. Use import FirebaseAuth to do this.  So you want to create two functions one to login and one signup, in each of these functions will go the code to login a user and also create a user.

The login function will check to see if the text fields is empty, if it is a message will be printed, otherwise if the error is equal to nil it will try to login with the email and password, if it fails it will print an error message.

func login(){
        
        if self.email.text == "" || self.pass.text == "" {
            
          print("Please Enter Some Text")
            
        } else {
            
            Auth.auth().signIn(withEmail: self.email.text!, password: self.pass.text!) { (user, error) in
                
                if error == nil {

                   print("You have successfully logged in")
              
                    
                } else {
    
                    print("Error")
                }
            }
        }
        
    }

The Sign-Up function will check to see if the email field is empty if it is a error message will be printed, otherwise if there is no error the method will create a user with the email and password text field.

 func signUp(){
        
        if email.text == "" {
            
            print("Error")
            
        } else {
            Auth.auth().createUser(withEmail: email.text!, password: pass.text!) { (user, error) in
                
                if error == nil {
                    print("You have successfully signed up")
                } else {
                    print(error)
                }
            }
        }
        
    }

In the action for your button you want to call your Login and Sign-Up functions. You want to use the Segmented Control to control weather the user is logging in or registering, use the index to do this.

 @IBAction func btn_clicked(_ sender: Any) {
        
        if seg.selectedSegmentIndex == 0 {
            
            login()
            
        } else if seg.selectedSegmentIndex == 1 {
            
            signUp()
            
        }
        
    }

 

Click Build & Run and your Login and Sign-Up page should be working. You will first have to register a user, if you want to check if everything works correctly.

Enter a email and password, select “Register” in your segmented control and it should register the user to the Firebase account connected.

So if you try to login with your user details and everything should work perfectly.

If you came across any error feel free to leave a comment and we will get back to you.

Post a Comment

You don't have permission to register