accountsjs API Reference

website·Indexed 33 days ago

https://www.accountsjs.com/

API documentation for accountsjs, featuring interfaces and classes for user authentication, session management, two-factor authentication, and database interfaces. Includes details on the AccountsServer, REST client, and various identity services such as magic links and password-based logins.

Tokens
57.4K
Snippets
457
Records
584
Agent score
67%

What's inside accountsjs

  1. Overview of accounts-js database adapters

    accounts-js uses a pluggable database architecture. Any database can be integrated by implementing the DatabaseInterface common interface exported by the @accounts/types package. This allows developers to create custom adapters if an official one is not available for their specific database.
  2. Overview of the @accounts authentication suite

    The @accounts suite provides an end-to-end authentication and accounts management solution. It supports password-based accounts, two-factor authentication, recovery options, and customizable account creation/validation. It is designed to be flexible regarding how data is transported and stored.
  3. Choose a transport for accounts-js

    accounts-js is transport-flexible and currently provides official packages for both GraphQL and REST (Express) implementations.
  4. AuthenticationService Interface Overview

    The AuthenticationService interface manages user authentication and the configuration of session and user data stores. It contains properties for the server instance and service identification, and methods to perform authentication and link database interfaces.
  5. Retrieve the currently authenticated user

    The getUser method returns information about the currently logged-in user. The user must be authenticated for this call to succeed.
    await accountsClient.getUser();
  6. IContext interface properties reference

    The IContext interface provides metadata about the current request and user session. It contains the following properties:

    • authToken (optional string): The authentication token associated with the request.
    • infos: An object of type ConnectionInformations containing connection details.
    • ip (string | null): The IP address of the client.
    • user (optional IUser): The user object associated with the session.
    • userAgent (string | null): The user agent string of the client.
    • userId (optional string): The unique identifier of the user.
    interface IContext {
      authToken?: string;
      infos: ConnectionInformations;
      ip: string | null;
      user?: IUser;
      userAgent: string | null;
      userId?: string;
    }
  7. Delegate authentication queries to a remote Accounts.js schema

    When using a microservice architecture, you can delegate queries (like me) to the remote accounts server using delegateToSchema. To ensure the remote server can identify the user, forward the authToken from the local context in the Authorization header of the remote request.
    // Resolver delegation
    me: {
      resolve: (parent, args, context, info) => {
        return delegateToSchema({
          schema: remoteSubschema,
          operation: OperationTypeNode.QUERY,
          fieldName: 'getUser',
          args, context, info,
        });
      },
    },
    
    // Remote executor forwarding token
    const remoteExecutor: AsyncExecutor = async ({ document, variables, context }) => {
      const fetchResult = await fetch(accountsServerUri, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          ...(context?.authToken && { Authorization: `Bearer ${context.authToken}` }),
        },
        body: JSON.stringify({ query: print(document), variables }),
      });
      return fetchResult.json();
    };
  8. Verify a user's email address

    Email verification is a two-step process: first, send a verification link using requestVerificationEmail(email), then call verifyEmail(token) using the token extracted from the verification URL.
    // Step 1: Request verification email
    await passwordClient.requestVerificationEmail('user@example.com');
    
    // Step 2: Verify email using token from URL
    await passwordClient.verifyEmail('token_from_url');
  9. Verify a user's email address

    Email verification is a two-step process: request a verification email containing a secret token, then verify that token to mark the email as verified.
    // Step 1: Send verification email
    await accountsPassword.requestVerificationEmail('email');
    
    // Step 2: Mark email as verified using the token
    await accountsPassword.verifyEmail('token');
  10. Initialize the AccountsPassword class

    The AccountsPassword class is used to manage password-based authentication. It can be instantiated with optional configuration and database connections.
    const auth = new AccountsPassword(options, db, dbSessions, server);