🗄️🔐 DatabaseKit + AuthKit
Restrict Features with Auth
import FirebaseKit

Restrict Features with Auth

We wanted to make Auth as simple as possible. If you need to make a view only accessible to authenticated users, you can use

Text("Sensitive Text")
    .requireLogin()

If the user is logged in, the View will just be displayed, otherwise, we show a sheet with SignInView, which allows the user to sign in, create an account, or reset their password.

Available Sign-In Methods:

  • Email/Password
  • Sign in with Apple

Sign In Flow

Execute Code only if Logged In

You can make certain code only execute if the user is logged in by using one of the following functions

User is signed in?

executeIfSignedIn(otherwise consequence: NotSignedInConsequence = .showInAppNotification,
                  _ closure: () -> Void)
  • otherwise - (Optional) action to perform in case the user is not signed in. showInAppNotification or showSignInScreen (Defaults to showInAppNotification).
  • closure - What to execute if the user is signed in.

Note: async version of this function is also available by just using await.

Usage Example:

func likePost() {
  db.executeIfSignedIn {
    // Code to like the post
  }
}

Specific user is signed in?

executeIfSignedIn(withUserID userID: String?,
                  otherwise consequence: NotSignedInConsequence = .showInAppNotification,
                  _ closure: () -> Void)
  • withUserID - Check if the current user is signed in and is the user with the specified userID.
  • otherwise - (Optional) action to perform in case the user is not signed in. .showInAppNotification or .showSignInScreen (Defaults to .showInAppNotification).
  • closure - What to execute if the user is signed in.

Note: async version of this function is also available by just using await.

Usage Example:

func printUsersSecretMessages(of userID: String) async {
  await db.executeIfSignedIn(withUserID: userID) {
    // Database request to get the secret messages,
    // which only the user with `userID` should be able to see
  }
}