zerot
    Preparing search index...

    Function auth

    • Creates an authentication condition that checks if a user is logged in and has a required role.

      Parameters

      • OptionalrequiredRole: string

        An optional role string. If provided, the user must have this role.

      Returns (input: any, context: AuthContext) => Promise<boolean>

      A condition function that takes input and authentication context, and returns a Promise resolving to a boolean.

      If authentication is required but no user is logged in, or if the session has expired, or if the user does not have the required role.

      class AdminService {
      @contract({
      requires: [auth("admin")],
      })
      async deleteCriticalData(dataId: string, context: AuthContext) {
      // Only accessible by users with the "admin" role
      console.log(`Deleting critical data ${dataId} by admin ${context.user?.id}`);
      }
      }

      class UserService {
      @contract({
      requires: [auth()], // Requires any logged-in user
      })
      async getUserProfile(userId: string, context: AuthContext) {
      // Accessible by any logged-in user
      console.log(`Fetching profile for user ${userId}`);
      }
      }