Skip to content

Ashish Kakkad

Technology Makes A Wonderful World

  • Home
  • SwiftUI
  • Objective-C
  • Swift Language
  • About

Email and password based authentication with Firebase in Swift

February 7, 2021April 28, 2017 by Ashish Kakkad

Firebase Authentication gives us backend services to authenticate users with your app. It provides SDKs and ready-made UI libraries. It supports authentication using passwords, and other providers like Google, Facebook and Twitter, Github, and more.
firebase-authentication

Steps to configure your app with Firebase

  • Open Firebase Console
  • Create new project over there
  • Create new app into the project
  • You will be getting GoogleService-Info.plist file from settings
  • Add it to the project
  • Add just one line to configure your app FIRApp.configure()

Swift
1
2
3
4
5
6
7
8
9
10
import UIKit
import Firebase
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        FIRApp.configure()
        return true
    }
}

Let’s step with Firebase Authentication

Step 1. Enable Email and Password authentication on console

Go to Firebase Console and open authentication tab. In authentication tab you will get 3 more tab Users, Sign-In Method and Email templates. We have to do Email and Password authentication so just enable that from the Sign-In Method tab.

Email password on console
Email password on console

Step 2. Jump on code by Create User/Register User in Firebase system

Firebase provides a simple method to create a user with email and password. Firebase will create a user and it will return Firebase user object if it got success else it will return an error object.
FIRAuth have createUser method which has arguments for email and password and it accepts the string value. It will return user and error object in block/closure.

Swift
1
2
3
4
5
6
7
8
FIRAuth.auth()?.createUser(withEmail: "USER@EMAIL.COM", password: "USER_PASSWORD") { (user, error) in
    if let error = error {
        print(error.localizedDescription)
    }
    else if let user = user {
        print(user)
    }
}

Create User/Register User
Create User/Register User

Following types of errors should return when you registering or creating a user:

  • The password must be 6 characters long or more.
  • The email address is already in use by another account.

Step 3. Login/Sign In with email and password

Firebase provides a method to sign in user with email and password. The method will return error and user object with closure/block. The method has two arguments email and password.

Swift
1
2
3
4
5
6
7
8
FIRAuth.auth()?.signIn(withEmail: "USER@EMAIL.COM", password: "USER_PASSWORD") { (user, error) in
    if let error = error {
        print(error.localizedDescription)
    }
    else if let user = user {
        print(user)
    }
}

Firebase Sign In/Login
Firebase Sign In/Login

Following types of errors should return when you sign in with firebase authentication:

  • The email address is badly formatted.
  • The password is invalid or the user does not have a password.
  • There is no user record corresponding to this identifier.

Step 4. Listen to the events of login with authentication state change listener

Firebase provides authentication state did change listener, which will listen if any changes occur in FIRAuth (Firebase Authentication Shared Instance Object).
Declaration:

Swift
1
var handle: FIRAuthStateDidChangeListenerHandle?

A method called addStateDidChangeListener used to add a listener on FIRAuth object which will result in FIRAuth and User object in closure/block.
Execution:

Swift
1
2
3
4
5
6
7
8
9
10
11
override func viewWillAppear(_ animated: Bool) {
    handle = FIRAuth.auth()?.addStateDidChangeListener() { (auth, user) in
        print(auth)
        if let user = user {
            print(user)
        }
    }
}
override func viewWillDisappear(_ animated: Bool) {
    FIRAuth.auth()?.removeStateDidChangeListener(handle!)
}

In above example, I have added listener when view will appear and removed that listener in view will disappear.


This is all about Email and password based authentication with Firebase. There are other methods available Google Sign In, Facebook login, GitHub authentication and more.
You can download the completed project with all of the code developed in this tutorial.
Hope you like this tutorial. Happy Coding 🙂

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Tumblr (Opens in new window) Tumblr
  • Click to share on Telegram (Opens in new window) Telegram
  • Click to share on Pocket (Opens in new window) Pocket
Categories iOS, iTuts, Swift Language Tags Apple, configure, createUser, FIRAuth, FIRAuth.auth(), FIRAuthStateDidChangeListenerHandle, Firebase, iOS, iOS 10, iPhone, iTuts, signIn, StateDidChangeListener, Swift, Swift 3, Swift 3.1, Swift Language, swiftlang, Xcode 8
What's new in Xcode 8.3?
Use of Codable and Coding Key with JSONEncoder and JSONDecoder in Swift 4
  • YouTube
  • Facebook
  • LinkedIn
  • GitHub
  • Medium
  • Twitter
Ashish Kakkad

Ashish Kakkad

iOS Application Developer | Swift | Objective-C

View Full Profile →

Twitter

Follow @ashishkakkad8

YouTube

Mastodon

Recent Posts

  • How Xcode 16.4 Beta fixes the std::char_traits template error from 16.3?
  • Xcode 16.3 URLSession Bug – How to Fix and Prevent It
  • How to add animation to MeshGradient in SwiftUI?
  • Apple Event October 2023 – A Recap of the Biggest Announcements
  • What’s new for developers today with iOS?

Archives

  • April 2025
  • March 2025
  • July 2024
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • May 2023
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • September 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • October 2019
  • September 2019
  • June 2019
  • April 2019
  • February 2019
  • October 2018
  • August 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • May 2015
  • April 2015
  • February 2015
  • January 2015
  • December 2014
  • October 2014

Categories

  • Apple News
  • AppStoreConnect
  • iOS
  • iOS 10
  • iOS 11
  • iOS 13
  • iOS 14
  • iOS 16
  • iOS 18
  • iOS 8
  • iOS 9
  • iTuts
  • Objective-C
  • Swift 2
  • Swift 3
  • Swift 4
  • Swift 4.2
  • Swift 5
  • Swift Language
  • Swift Resources
  • SwiftUI
  • WatchKit
  • Xcode
    © 2025 Ashish Kakkad • Built with GeneratePress