Blade Authentication Utilities

3.29.10 · active · verified Wed Apr 22

The `blade-auth` package provides essential utility functions and helpers designed for integrating authentication services seamlessly into applications built with the Blade framework. As of version 3.29.10, it offers streamlined mechanisms for common authentication patterns, including user registration, sign-in, sign-out, and active session management. The package maintains a relatively frequent release cadence, with recent updates focusing on refining internal 'trigger' syntax, improving documentation accuracy, and enhancing developer experience through features like dependency override support in the build API and improved error handling. Its primary differentiation lies in its deep integration within the Blade ecosystem, aiming to abstract away common authentication complexities and provide a consistent experience aligned with the Blade architectural philosophy. It targets modern JavaScript environments, likely favoring ESM.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the initialization of `blade-auth`, a user signup and sign-in flow, retrieving the current session, and finally signing out.

import { initAuth, signIn, signUp, signOut, getAuthSession } from 'blade-auth';

interface AuthConfig {
  apiBaseUrl: string;
  jwtSecret: string;
}

interface UserCredentials {
  email: string;
  password: string;
}

async function runAuthFlow() {
  // Initialize auth with configuration
  const config: AuthConfig = {
    apiBaseUrl: process.env.BLADE_API_URL ?? 'https://api.example.com/v1',
    jwtSecret: process.env.BLADE_JWT_SECRET ?? 'super-secret-key-please-change'
  };
  
  // In a real app, this would be configured once, likely on app start
  initAuth(config);

  const user: UserCredentials = {
    email: 'test@example.com',
    password: 'password123'
  };

  try {
    console.log('Attempting to sign up...');
    const signUpResult = await signUp(user.email, user.password);
    console.log('Sign up successful:', signUpResult);

    console.log('Attempting to sign in...');
    const signInResult = await signIn(user.email, user.password);
    console.log('Sign in successful:', signInResult);

    console.log('Getting current auth session...');
    const session = await getAuthSession();
    console.log('Current session:', session);

    if (session?.isAuthenticated) {
      console.log('User is authenticated. Token:', session.token);
    }

    console.log('Attempting to sign out...');
    await signOut();
    console.log('Sign out successful.');

    const postSignOutSession = await getAuthSession();
    console.log('Session after sign out:', postSignOutSession);

  } catch (error) {
    console.error('Authentication error:', error);
  }
}

runAuthFlow();

view raw JSON →