Email and password based authentication with Firebase in Swift
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.
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()
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.
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.
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)
}
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.
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)
}
}
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.
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.
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)
}
}
Following types of errors should return when you sign in with firebase authentication:
if let error = error
print(error.localizedDescription)
}
else if let user = user
{
print(user)
}
}
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:
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:
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.
Firebase provides authentication state did change listener, which will listen if any changes occur in FIRAuth (Firebase Authentication Shared Instance Object).
Declaration:
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:
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.
Declaration:
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:
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.




Comments
Post a Comment