📦 SwiftyLaunch Modules
🔐 AuthKit
Lock Actions to Signed-In Users

Lock Actions to Signed-In Users

AuthKit (SwiftyLaunch Module) - Lock Actions behind Auth

Maybe you want to lock specific actions to only signed-in users in your app. For this, SwiftyLaunch provides a couple of ways to do this.

By directly checking the auth state

You can simply check if the user has premium access, by checking the authState variable of the DB object.

import FirebaseKit
import SharedKit
 
/// Will vibrate if the user is logged in
func vibrateForLoggedInUsers(db: DB) {
    if db.authState == .signedIn {
        Haptics.notification(type: .success)
    }
}
FirebaseBackend.swift
public class DB: ObservableObject {
    @Published public var authState: AuthState = .signedOut
}
 
public enum AuthState {
    case signedOut           // User is signed out
    case signedInUnverified  // User is signed in but hasn't verified his email address yet
    case signedIn            // User is signed in
}

Recommended: By calling the executeIfSignedIn() function

We recommend to use the executeIfSignedIn() function instead. Just pass whatever needs to be executed if the user is signed in.

If the user isn't signed in or hasn't confirmed his email address yet, the function will present the user with the sign in sheet or an in-app notification stating that the user needs to be signed in.

import FirebaseKit
import SharedKit
 
/// Will vibrate if the user is logged in
func vibrateForPremiumUsers(db: DB) {
    db.executeIfSignedIn {
        Haptics.notification(type: .success)
    }
}

The function signature is as follows:

FirebaseBackend.swift
public class DB: ObservableObject {
    public func executeIfSignedIn(
        otherwise consequence: NotSignedInConsequence = .showInAppNotification,
        _ closure: () -> Void
    ) { }
}
  • otherwise - (Optional) action to perform in case the user is not signed in. .showInAppNotification to show an in-app notification indication that the user must be logged in or .showSignInScreen to directly show the sign-in sheet (Defaults to .showInAppNotification).
  • closure - What to execute if the user is logged in

The function comes in two flavors: synchronous and asynchronous. The async version is available by just using await.

Additionally, if you want to lock features to specific users, you may want to use the executeIfSignedIn(withUserID:) function.

import FirebaseKit
import SharedKit
 
// will only open the profile settings if the user is signed in
// and the user ID matches the provided one
func openProfileSettings(forUser userID: String, db: DB String) {
    db.executeIfSignedIn(withUserID: userID) {
        // ... open profile settings
    }
}

The definition of the function is as follows:

FirebaseBackend.swift
public class DB: ObservableObject {
    public func executeIfSignedIn(
        withUserID userID: String?,
        otherwise consequence: NotSignedInConsequence = .showInAppNotification,
        _ closure: () -> Void
     ) { }
}
  • userID - The user ID to check against. If user isn't logged in or the user ID doesn't match, the otherwise consequence will be executed.
  • otherwise - (Optional) action to perform in case the user is not signed in. .showInAppNotification to show an in-app notification indication that the user must be logged in or .showSignInScreen to directly show the sign-in sheet (Defaults to .showInAppNotification).
  • closure - What to execute if the user is logged in
⚠️

Do not, I repeat, DO NOT use this as a security measure. It's a nice convenience feature, and a "first layer of defense", but always validate the user's permissions on the server-side. You can use AuthKit-related features on the backend to achieve just that.