📦 SwiftyLaunch Modules
⚙️ BackendKit
AuthKit Integration

AuthKit Integration in BackendKit

AuthKit Integration

Access authentication details (user id, auth status) of the requesting user using AuthKit.

Basic Usage

Accessing Authentication Details of the Requesting User

If a user sends a request to your backend from your mobile client, you can easily check if the user is authenticated and get their user ID by using built in Firebase Auth parameters

export const getMyUserID = onCall(async (request) => {
  let requestingUserID = request.auth?.uid; // string or undefined
  return {
    // return null if user is not authenticated (undefined is not really suitable here)
    userID: requestingUserID ?? null,
  };
});

Access to list, delete, and create users in Firebase Auth

You can also perform user-related operations on AuthKit, by using Firebase Auth's getAuth function.

Import getAuth

import { getAuth } from "firebase-admin/auth";

Usage Example

There are many operations you can perform with Firebase Auth. Here is an example of how you can list all users in Firebase Auth.

export const getAllUsers = onCall(async (request) => {
  try {
    const users = await getAuth().listUsers();
 
    let usersArray: {
      userID: string;
      username: string;
    }[] = [];
 
    users.users.forEach((user) => {
      usersArray.push({
        userID: user.uid,
        username: user.displayName || "NO_DISPLAY_NAME",
      });
    });
 
    return usersArray;
  } catch (error) {
    throw new Error("Server Error");
  }
});

There are also other functions you can use to create, delete, and update users. Check out the Firebase Auth documentation (opens in a new tab) for more information.