Swift Firebase Signup and Login Tutorial
In this tutorial we will be setting up a Firebase login using an email address. We will also additionally allow for users to sign up with their email
Setting up Firebase Cocoapods
First of all create a new Single View application xcode project. Once you have set it up navigate to the project in your terminal and initialize CocoaPods with pod init. After this we need to add the following CocoaPod libraries to our Podfile:
pod ‘Firebase/Auth’
Our whole Podfile will look something like the following, in my case the project name is ‘GSign’
# Uncomment the next line to define a global platform for your project # platform :ios, '9.0' target 'GSign' do # Comment the next line if you're not using Swift and don't want to use dynamic frameworks use_frameworks! # Pods for GSign pod 'GoogleSignIn' pod 'Firebase/Auth' target 'GSignTests' do inherit! :search_paths # Pods for testing end target 'GSignUITests' do inherit! :search_paths # Pods for testing end end
Coding our Firebase Login!
Now that we have our CocoaPods setup, it’s time to code our Firebase Login, we will be following the patterns outlined here to implement our Login. Head over to our storyboard and add buttons and labels until you have an interface as follows:
With our UI setup open up the assistant editor and connect our storyboard objects as follows:
Outlets
txtEmail for the Email Address textbox
txtPassword for the Password textbox
lblStatus for the Status labe
Actions
login for the Login button
createAccount for the Create Account butto
Now before you leave the storyboard select the password textbox, select the attributes inspector and change the content type to Password and tick ‘Secure Text Entry’. This will ensure no-one peeking over the shoulder of a user can see the password 🙂
First of all after this go into the AppDelegate.swift and add the following under import UIKit
import Firebase
Then in application didFinishLaunching with options add the following to setup Firebase
FirebaseApp.configure()
Now go to your ViewController.swift, under import UIKit add the following again:
import Firebase
Now in the actions we created code them as follows:
@IBAction func login(_ sender: Any) { let email = txtEmail.text! let password = txtPassword.text! Auth.auth().signIn(withEmail: email, password: password) { [weak self] user, error in guard let strongSelf = self else { return } if(error != nil) { strongSelf.lblStatus.text = "Error, check username and password" print(error) return } strongSelf.lblStatus.text = "Login sucesss for email \(email)" } }
This will take the email and password and login with firebase. If there is an error it will set the status to show error and print out the error to the console.
On a successful login the status message will show success.
Now for our create account action, set it up as follows:
@IBAction func createAccount(_ sender: Any) { let email = txtEmail.text! let password = txtPassword.text! Auth.auth().createUser(withEmail: email, password: password) { authResult, error in if(error != nil) { self.lblStatus.text = "Error" print(error) return } self.lblStatus.text = "User created!" } }
This will create an account with a given username and password.
Now go ahead and test the app! Create your own account and then use it to login. Also try out the following scenarios to see what happens!
- Login with the wrong username or password
- Create an account that already exists
Further reading
Congratulations on setting up a Firebase email login in your Swift app. For further setup I would highly recommend to check out the following:
https://firebase.google.com/docs/auth/ios/manage-users
In particular ‘Send a user a verification email’, you can send a user a verification email with the following:
Auth.auth().currentUser?.sendEmailVerification { (error) in // ... }
Additional you can check if the user has validated their email address with the following line of code:
let isValidEmail = Auth.auth().currentUser?.isEmailVerified
The final useful nugget of info for emails is templates, these allow you to modify the emails sent for email verification, password reset, email change and so on. You can find this under the Authentication Menu on the Templates tab.