# Subscribe to User Events

Subscribe to events for a specific user (requires authentication)

User-related events including authentication, profile changes, and activity tracking.

Security Note: These events contain sensitive user information and require proper authentication.


Scalar Galaxy Events sends these messages to you.

Address: `users/{userId}/events`

## Servers

- `wss://galaxy.scalar.com` (Production, wss)
- `ws://localhost:8080` (Development, ws)
- `kafka://kafka.galaxy.scalar.com:9092` (Event Bus, kafka)
- `mqtt://mqtt.galaxy.scalar.com:1883` (Mqtt Bridge, mqtt)

## Authorization

Connecting to `production` takes one of:

- `bearerAuth` (http bearer): JWT Bearer token authentication for WebSocket connections
- `httpApiKey` (httpApiKey in query `api_key`): API key for WebSocket authentication

The operation takes one of:

- `bearerAuth` (http bearer): JWT Bearer token authentication for WebSocket connections
- `httpApiKey` (httpApiKey in query `api_key`): API key for WebSocket authentication

## Address Parameters

- `userId`: The ID of the user.

## Messages

### User Signed Up Event

A new user has registered in the Scalar Galaxy

Published when a user successfully creates an account

Content type: `application/json`

Payload: `UserSignedUpPayload`

Example payload:

```json
{
  "eventId": "evt_1234567897",
  "eventType": "user.signed_up",
  "timestamp": "2024-01-15T18:30:00Z",
  "user": {
    "id": 100,
    "name": "Alice",
    "email": "alice@example.com"
  },
  "registration": {
    "method": "email",
    "source": "web",
    "ipAddress": "192.168.1.100"
  },
  "metadata": {
    "source": "rest-api",
    "requestId": "req_mno345"
  }
}
```

### User Authenticated Event

A user has successfully authenticated

Published when a user logs in or refreshes their token

Content type: `application/json`

Payload: `UserAuthenticatedPayload`

Example payload:

```json
{
  "eventId": "evt_1234567898",
  "eventType": "user.authenticated",
  "timestamp": "2024-01-15T19:00:00Z",
  "user": {
    "id": 1,
    "name": "Marc"
  },
  "authentication": {
    "method": "bearer_token",
    "tokenType": "JWT",
    "expiresAt": "2024-01-16T19:00:00Z"
  },
  "session": {
    "id": "sess_abc123",
    "ipAddress": "192.168.1.100",
    "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)"
  },
  "metadata": {
    "source": "auth-service",
    "requestId": "req_pqr678"
  }
}
```

### User Profile Updated Event

A user's profile information has been modified

Published when user profile data is updated

Content type: `application/json`

Payload: `UserProfileUpdatedPayload`

Example payload:

```json
{
  "eventId": "evt_1234567899",
  "eventType": "user.profile_updated",
  "timestamp": "2024-01-15T19:30:00Z",
  "user": {
    "id": 1,
    "name": "Marc Updated"
  },
  "changes": [
    {
      "field": "name",
      "oldValue": "Marc",
      "newValue": "Marc Updated"
    }
  ],
  "metadata": {
    "source": "rest-api",
    "userId": 1,
    "requestId": "req_stu901"
  }
}
```

### User Deleted Event

A user account has been deleted

Published when a user account is permanently removed

Content type: `application/json`

Payload: `UserDeletedPayload`

Example payload:

```json
{
  "eventId": "evt_1234567900",
  "eventType": "user.deleted",
  "timestamp": "2024-01-15T20:00:00Z",
  "user": {
    "id": 99,
    "name": "Deleted User"
  },
  "deletion": {
    "reason": "user_request",
    "dataRetention": "30_days"
  },
  "metadata": {
    "source": "user-service",
    "adminId": 1,
    "requestId": "req_vwx234"
  }
}
```

### Login Attempt Event

A user login attempt has been made

Published for both successful and failed login attempts

Content type: `application/json`

Payload: `LoginAttemptPayload`

Example payload:

```json
{
  "eventId": "evt_1234567901",
  "eventType": "login.attempt",
  "timestamp": "2024-01-15T20:30:00Z",
  "attempt": {
    "email": "marc@scalar.com",
    "success": false,
    "reason": "invalid_credentials",
    "ipAddress": "192.168.1.200",
    "userAgent": "curl/7.68.0"
  },
  "security": {
    "riskScore": 0.3,
    "suspiciousActivity": false
  },
  "metadata": {
    "source": "auth-service",
    "requestId": "req_yza567"
  }
}
```

## Types

```ts
export type SubscribeToUserEventsMessage =
  | UserSignedUpPayload
  | UserAuthenticatedPayload
  | UserProfileUpdatedPayload
  | UserDeletedPayload
  | LoginAttemptPayload;

export interface UserSignedUpPayload {
  eventId: string;
  eventType: "user.signed_up";
  timestamp: string;
  user: User;
  registration?: {
    method?: "email" | "oauth" | "sso";
    source?: "web" | "mobile" | "api";
    ipAddress?: string;
  };
  metadata?: EventMetadata;
}

export interface UserAuthenticatedPayload {
  eventId: string;
  eventType: "user.authenticated";
  timestamp: string;
  user: User;
  authentication: {
    method: "bearer_token" | "basic_auth" | "oauth" | "api_key";
    tokenType: "JWT" | "OAuth" | "Basic";
    expiresAt?: string;
  };
  session?: {
    id?: string;
    ipAddress?: string;
    userAgent?: string;
  };
  metadata?: EventMetadata;
}

export interface UserProfileUpdatedPayload {
  eventId: string;
  eventType: "user.profile_updated";
  timestamp: string;
  user: User;
  changes: FieldChange[];
  metadata?: EventMetadata;
}

export interface UserDeletedPayload {
  eventId: string;
  eventType: "user.deleted";
  timestamp: string;
  user: User;
  deletion?: {
    reason?: "user_request" | "admin_action" | "policy_violation" | "inactive";
    dataRetention?: "immediate" | "30_days" | "90_days" | "1_year";
  };
  metadata?: EventMetadata;
}

export interface LoginAttemptPayload {
  eventId: string;
  eventType: "login.attempt";
  timestamp: string;
  attempt: {
    email: string;
    success: boolean;
    reason?: "invalid_credentials" | "account_locked" | "rate_limited" | "success";
    ipAddress?: string;
    userAgent?: string;
  };
  security?: {
    riskScore?: number;
    suspiciousActivity?: boolean;
  };
  metadata?: EventMetadata;
}

/** A user */
export interface User {
  readonly id?: number;
  name?: string;
}

export interface EventMetadata {
  /** Source system that generated the event */
  source?: string;

  /** ID of the user who triggered the event (if applicable) */
  userId?: number;

  /** ID of the request that triggered the event */
  requestId?: string;

  /** Correlation ID for tracing related events */
  correlationId?: string;

  /** Event severity level */
  severity?: "info" | "warning" | "error" | "critical";
}

export interface FieldChange {
  /** Name of the field that changed */
  field: string;

  /** Previous value of the field */
  oldValue: unknown;

  /** New value of the field */
  newValue: unknown;
}
```
