S SmartDocs
系列: angular typescript 112 行 · 更新於 2026-02-03

user.model.ts

angular/comprehensive-demo/src/app/models/user.model.ts

/**
 * User Model
 * 
 * This interface defines the structure of a User object in our application.
 * Using TypeScript interfaces provides:
 * - Type safety: Ensures data conforms to expected structure
 * - IntelliSense: Better IDE support and autocomplete
 * - Documentation: Serves as inline documentation for developers
 * - Compile-time checks: Catches errors before runtime
 */

export interface User {
  /**
   * Unique identifier for the user
   * Typically generated by the backend database
   */
  id: number;

  /**
   * User's first name
   */
  firstName: string;

  /**
   * User's last name
   */
  lastName: string;

  /**
   * User's email address - used for authentication and communication
   */
  email: string;

  /**
   * User's role in the system (e.g., 'admin', 'user', 'guest')
   * Used for role-based access control (RBAC)
   */
  role: 'admin' | 'user' | 'guest';

  /**
   * Whether the user account is currently active
   * Inactive users may be restricted from certain actions
   */
  isActive: boolean;

  /**
   * Timestamp when the user account was created
   * ISO 8601 format string
   */
  createdAt: string;

  /**
   * Timestamp when the user account was last updated
   * ISO 8601 format string
   */
  updatedAt: string;
}

/**
 * User Registration Data
 * 
 * Data structure for creating a new user account.
 * Note: This doesn't include fields like 'id' or 'createdAt' which are
 * generated by the backend.
 */
export interface UserRegistration {
  firstName: string;
  lastName: string;
  email: string;
  password: string;
  role?: 'admin' | 'user' | 'guest'; // Optional, defaults to 'user'
}

/**
 * User Login Credentials
 * 
 * Data structure for user authentication
 */
export interface LoginCredentials {
  email: string;
  password: string;
}

/**
 * Authentication Response
 * 
 * Structure of the response received after successful authentication
 */
export interface AuthResponse {
  /**
   * JWT (JSON Web Token) used for authenticated API requests
   * This token should be stored securely and included in HTTP headers
   */
  token: string;

  /**
   * Refresh token used to obtain a new access token when it expires
   * Typically has a longer expiration time than the access token
   */
  refreshToken: string;

  /**
   * User information returned after successful authentication
   */
  user: User;

  /**
   * Token expiration time in seconds
   */
  expiresIn: number;
}

相關文章