Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Get Started with EasyAuth

Ben Myers edited this page Feb 7, 2022 · 13 revisions

Overview

Welcome to EasyAuth! 🎉

EasyAuth makes it fast and easy to:

  • create user objects with pre-defined properties,
  • manage user details, including email, password, profile images, and unique usernames,
  • listen to user data updates from Firestore,
  • and sign in with third-party providers like Apple and Google.

Requisites

What You'll Learn

In this page, you'll learn about:

  1. TODO

Get Started

Setting Up your User Class

In your project structure, create a new file for your User class. Import EasyFirebase. Then, declare your class. Ensure that your user class extends the EasyUser open class.

ExampleUser.swift

import EasyFirebase

class ExampleUser: EasyUser {
  
}

Pre-Defined User Properties

The EasyUser open class has several pre-defined properties. These properties should save you time from having to implement your own sign-in system. In this article, we'll only focus on using a few of these properties (marked with a ⭐️).

Property Name Description Settable?
notifications The user's Cloud Messaging Notifications. Yes
disabledMessageCategories The user's disabled Cloud Messaging categories. Yes
progress The user's onboarding progress. Yes
deviceToken The user's unique Cloud Messaging device token. No
appVersion The user's most recent version string, determined by the "Version" field of the app target. No
lastSignon The user's last sign-on date. No
⭐️ email The user's email address. No
⭐️ username The user's unique username. No
⭐️ displayName The user's (not necessarily unique) display name. No
profileImageURL A URL to the user's profile image. No
index The index of the user. Starts at 0 and increments for each user created. No
⭐️ id The user's unique id, obtained from FirebaseAuth's generated user ID. No
dateCreated The date upon which the user was created. No

Adding Custom User Properties

Let's start by adding some custom properties to our ExampleUser class. In our example, we want to keep track of three things: the user's favorite food, their age, and whether they have a job.

ExampleUser.swift

class ExampleUser: EasyUser {
  var favoriteFood: String?
  var age: Int = -1
  var hasJob: Bool = false
}

Note that we define default values for each of these properties, except for the first. Doing so is necessary, except for Optional values.

⚠️ Note: All custom properties introduced to your own user object must be of types that conform to Codable.

Listening to Updates

Now, let's handle user data updates. EasyAuth handles a lot of the grunt work with auth state and user object updates. Any time the auth state changes, or any time the user object updates in Firestore, a callback to onUserUpdate is called. This also applies to the user's first sign-in, or account creation.

So, you'll need to start listening for these updates when your app launches. Wherever your app first launches, run the following code:

EasyAuth.onUserUpdate { user in
  // Check to make sure the `user` object passed in the closure is the right type, and is not `nil`
  guard let user = user as? ExampleUser else { return }
  // Set your global `user` instance used across the app
  global.user = user
}

⚠️ Note: As mentioned in Installation, you'll need to call EasyFirebase.configure() before running any of this code. Calling this directly before EasyAuth.onUserUpdate(perform:) is the best action.

To review, calling EasyAuth.onUserUpdate(perform:) will pass your user object to the closure when any of the following occur:

  • The user signs in or out
  • The user creates a new account
  • The user's data changes in Firestore