DataBase

Manages GunDB user authentication and various utility helpers for session, alias/username, SEA cryptography, event handling, and reactive streams.

Constructors

  • Constructs a new DataBase instance connected to a GunDB instance.

    Parameters

    • gun: IGunInstance

      The main GunDB instance.

    • Optionalcore: any

      Optionally, the root Gun instance (unused in this context).

    • Optionalsea: any

      Optional cryptography (Gun SEA) instance; will be auto-discovered if not provided.

    Returns DataBase

    If gun or gun.user() is not provided.

Properties

_isDestroyed: boolean = false

Whether the database instance has been destroyed

_rxjs?: RxJS

RxJS-based GunDB observable/stream helper

core?: any

ShogunCore instance for emitting events

crypto: crypto

Crypto utilities used internally

eventEmitter: EventEmitter

EventEmitter for app-specific event management

gun: IGunInstance

GunDB instance

onAuthCallbacks: AuthCallback[] = []

Registered callbacks for auth state changes

sea: any

Gun SEA cryptography context (usually gun.SEA)

user: null | IGunUserInstance<any, any, any, any> = null

Cached user instance or null if not logged in

usernamesNode: IGunChain<any, any, any, any>

Gun node dedicated to mapping usernames to pubkeys

Methods

  • Aggressively clean up authentication state and session. Typically used for error recovery.

    Returns void

  • Internal

    Assemble a standard AuthResult object after a successful login.

    Parameters

    • username: string

      Resulting username.

    • userPub: string

      Public key (pub) for logged-in user.

    Returns AuthResult

    AuthResult.

  • Tears down the DataBase instance and performs cleanup of all resources/listeners. No further actions should be performed on this instance after destruction.

    Returns void

  • Emit a custom event.

    Parameters

    • event: string | symbol

      Event name.

    • Optionaldata: any

      Optional associated data.

    Returns boolean

    true if listeners were notified; otherwise false.

  • Ensures that an alias/username is available in GunDB for registration.

    Parameters

    • alias: string

      Username to check.

    • timeout: number = 5000

      Timeout in milliseconds (default 5000ms).

    Returns Promise<void>

    If the alias is already taken.

  • Returns the currently authenticated user's public key and Gun user instance, if logged in.

    Returns null | { pub: string; user?: any }

    Object containing pub (public key) and optionally user, or null.

  • Get current user's public key.

    Returns null | string

    User's public key or null if not logged in.

  • Initialize the database instance.

    Returns void

  • Checks if a given alias/username is available on GunDB.

    Parameters

    • alias: string

      Username to check for availability.

    • timeout: number = 5000

      Timeout in ms (default: 5000).

    Returns Promise<boolean>

    Promise resolving to true if available; otherwise false.

    If alias is invalid or on I/O error.

  • Checks if a given alias/username is taken on GunDB.

    Parameters

    • alias: string

      Username to check for availability.

    Returns Promise<boolean>

    Promise resolving to true if taken; otherwise false.

    If alias is invalid or on I/O error.

  • Check if a user is currently logged in (there is a valid session).

    Returns boolean

    true if logged in; otherwise false.

  • Sign in (authenticate) as an existing user by username/password or SEA pair.

    Parameters

    • username: string

      Username to log in as.

    • password: string

      User's password (or "" if using pair).

    • Optionalpair: null | ISEAPair

      Optional cryptographic SEA pair.

    Returns Promise<AuthResult>

    AuthResult Promise.

  • Authenticate using a SEA pair directly (no password required).

    Parameters

    • username: string

      The user's username for identification (not cryptographically enforced).

    • pair: ISEAPair

      GunDB SEA pair for authentication.

    Returns Promise<AuthResult>

    Promise with authentication result.

    Authenticates user using a GunDB pair directly without password.

  • Legacy API: Sign in using a username and SEA pair (password parameter is unused).

    Parameters

    • username: string

      Username to sign in as.

    • pair: ISEAPair

      SEA key pair.

    Returns Promise<AuthResult>

    AuthResult Promise.

  • Log out the current user, clear local state and remove session from storage.

    Returns void

  • Internal

    Internal: notify all onAuth callbacks with current user.

    Parameters

    • pub: string

      User's public key (pub).

    Returns void

  • Remove an event handler.

    Parameters

    • event: string | symbol

      Event name.

    • listener: EventListener

      Listener function.

    Returns void

  • Register an event handler.

    Parameters

    • event: string | symbol

      Event name.

    • listener: EventListener

      Listener function.

    Returns void

  • Listen for authentication/sign-in events (login, logout, etc).

    Parameters

    • callback: AuthCallback

      Function to call with new user instance.

    Returns () => void

    Function to remove the registered callback.

  • Register an event handler for a single event occurrence.

    Parameters

    • event: string | symbol

      Event name.

    • listener: EventListener

      Listener function.

    Returns void

  • Register a new alias (username) → public key mapping on GunDB.

    Parameters

    • alias: string

      The username/alias to register.

    • userPub: string

      The user's public key.

    • timeout: number = 5000

      Timeout in ms (default 5000).

    Returns Promise<void>

    If alias/userPub is invalid or the alias cannot be registered.

  • Internal

    Reset gun.user() authentication state and clear cached user.

    Returns void

  • Attempt to restore a previously saved session from sessionStorage.

    Returns { error?: string; success: boolean; userPub?: string }

    Object indicating success, error, and userPub if restored.

  • Returns the bound RxJS GunDB helper (reactive streams).

    Returns RxJS

    RxJS instance.

  • Save credentials for the current session to sessionStorage, if available.

    Parameters

    • userInfo: { alias: string; pair: ISEAPair; userPub: string }

      The credentials and user identity to store.

    Returns void

  • Register and authenticate a new user account.

    Parameters

    • username: string

      The username to create/account for.

    • password: string

      The user's password.

    • Optionalpair: null | ISEAPair

      Optional cryptographic pair (for auth instead of password).

    Returns Promise<SignUpResult>

    SignUpResult Promise.

  • Internal

    Internal: subscribe to GunDB "auth" events and notify listeners. Listeners are invoked on authentication status change.

    Returns void

  • Validate that a provided password meets minimum length requirements.

    Parameters

    • password: string

      Password string to validate.

    Returns { error?: string; valid: boolean }

    Object indicating validity and, if invalid, an error.

  • Validate a signup request's username, password, and/or cryptographic pair.

    Parameters

    • username: string

      Username string.

    • password: string

      Password string.

    • Optionalpair: null | ISEAPair

      Optional cryptographic SEA pair.

    Returns { error?: string; valid: boolean }

    Object with validation status and optional error.