Skip to main content

Overview

The Authentication API (db.auth) provides methods for user login, logout, session management, and user profile operations. All authentication data is stored in localStorage with the key cafh_user_session_v1.

Methods

login

Authenticates a user with email and password credentials.
db.auth.login(email: string, pass: string): User | null
email
string
required
User’s email address
pass
string
required
User’s password
return
User | null
Returns a User object if authentication succeeds, or null if credentials are invalid
const user = db.auth.login('[email protected]', 'admin123');
if (user) {
  console.log(`Welcome ${user.name}`);
  console.log(`Role: ${user.role}`);
} else {
  console.log('Invalid credentials');
}

logout

Clears the current user session.
db.auth.logout(): void
db.auth.logout();
console.log('Session cleared');

getCurrentUser

Retrieves the currently authenticated user from session storage.
db.auth.getCurrentUser(): User | null
return
User | null
Returns the current User object if a session exists, or null if no user is logged in
const currentUser = db.auth.getCurrentUser();
if (currentUser) {
  console.log(`Logged in as: ${currentUser.name}`);
  console.log(`Role: ${currentUser.role}`);
  console.log(`Tenant: ${currentUser.tenantId}`);
}

getAllUsers

Retrieves all users in the system (demo data).
db.auth.getAllUsers(): User[]
return
User[]
Returns an array of all User objects in the system
const allUsers = db.auth.getAllUsers();
console.log(`Total users: ${allUsers.length}`);

updateCurrentUser

Updates the current user’s profile information.
db.auth.updateCurrentUser(updates: Partial<User>): User | null
updates
Partial<User>
required
Object containing the user fields to update
return
User | null
Returns the updated User object, or null if no session exists
const updatedUser = db.auth.updateCurrentUser({
  name: 'Juan Pérez',
  phone: '+56 9 1234 5678',
  city: 'Santiago',
  interests: ['Meditación', 'Yoga', 'Retiros']
});

if (updatedUser) {
  console.log('Profile updated successfully');
}

User Roles

The system supports the following user roles via the UserRole enum:
  • SUPER_ADMIN - Full system access
  • ADMIN - Administrative access
  • EDITOR - Content editing access
  • MEMBER - Standard member access
  • GUEST - Limited guest access
import { UserRole } from './types';

const user = db.auth.getCurrentUser();
if (user?.role === UserRole.SUPER_ADMIN) {
  // Admin-only functionality
}

Storage Key

  • Key: cafh_user_session_v1
  • Location: localStorage
  • Format: JSON-serialized User object
See Data Types for the complete User interface definition.

Build docs developers (and LLMs) love