Backend Architecture
Relevant source files
The following files were used as context for generating this wiki page:The backend for the aidoit4u mobile app is a Node.js server built with Express. It serves as an API gateway, handling authentication, data persistence, and communication with external services. Key responsibilities include managing user sessions via Manus OAuth, interacting with a MySQL database through the Drizzle ORM, and proxying requests to specialized services for file storage and Large Language Model (LLM) interactions.
The architecture is designed to support both web and mobile clients, with distinct authentication flows for each platform. It uses tRPC for type-safe API routes and relies on environment variables for configuration, including credentials for various services.
Core Components & Technology Stack
The backend is composed of a web server, a database layer, and integrations with external APIs. The primary technologies are identified from the project's package.json.
This diagram shows the high-level interaction between the client applications, the backend server, the database, and external services.
Technology Stack
The following table lists the key libraries and frameworks used in the backend.
| Category | Technology | Description |
|---|---|---|
| Runtime | Node.js | JavaScript runtime environment. |
| Web Framework | Express | Web application framework for Node.js, used for routing and middleware. |
| API | tRPC (@trpc/server) | Toolkit for building type-safe APIs between client and server. |
| Database ORM | Drizzle ORM (drizzle-orm, drizzle-kit) | TypeScript ORM for interacting with the database. |
| Database | MySQL (mysql2) | Relational database for data persistence. |
| Authentication | jose | Library for JWT signing and verification, used for session management. |
| HTTP Client | axios | Promise-based HTTP client for making requests to external services. |
| Environment | dotenv | Loads environment variables from a .env file. |
| Tooling | tsx, esbuild | For running and bundling TypeScript code. |
Sources: package.json:5-10,20-48
Authentication and Authorization
Authentication is handled via Manus OAuth. The backend implements OAuth callback handlers and manages user sessions using JSON Web Tokens (JWT).
Sources: server/_core/oauth.ts, server/_core/sdk.ts
Manus OAuth Integration
The server provides endpoints to handle the OAuth 2.0 authorization code grant flow. The server/_core/sdk.ts file contains the OAuthService class which communicates with the Manus OAuth server to exchange an authorization code for an access token and then fetch user information.
The sequence diagram below illustrates the authentication flow for both web and mobile clients.
Session Management
Once a user is authenticated, the backend creates a session token using the jose library. This JWT contains the user's openId, appId, and name.
createSessionToken(openId): Signs a new JWT that expires in one year.verifySession(token): Verifies the signature and expiration of a given JWT.authenticateRequest(req): A middleware-like function that authenticates an incoming request by checking for aBearertoken in theAuthorizationheader or a session cookie. It verifies the token, retrieves the user from the database, and automatically syncs user info from the OAuth server if the user doesn't exist locally.
Sources: server/_core/sdk.ts:143-241
Auth API Endpoints
The following REST endpoints are registered in the Express application to handle authentication.
| Method | Path | Description |
|---|---|---|
GET | /api/oauth/callback | Handles OAuth callback for web clients, exchanges code for a token, and sets a session cookie. |
GET | /api/oauth/mobile | Handles OAuth callback for mobile clients, returns a session token and user info as JSON. |
POST | /api/auth/logout | Clears the session cookie to log the user out. |
GET | /api/auth/me | Returns the currently authenticated user's information based on the session token or cookie. |
POST | /api/auth/session | Establishes a session cookie from a Bearer token, used by web clients in an iframe. |
Sources: server/_core/oauth.ts:49-166
Database Layer
The backend uses Drizzle ORM to interact with a MySQL database. The schema is defined in a type-safe manner using TypeScript.
Sources: drizzle/schema.ts, drizzle/meta/0000_snapshot.json
Schema Definition
The primary table is users, which stores user information synced from the Manus OAuth provider.
The ER diagram below shows the structure of the users table.
The table schema is defined in drizzle/schema.ts:
// Source: drizzle/schema.ts:7-20
export const users = mysqlTable("users", {
id: int("id").autoincrement().primaryKey(),
openId: varchar("openId", { length: 64 }).notNull().unique(),
name: text("name"),
email: varchar("email", { length: 320 }),
loginMethod: varchar("loginMethod", { length: 64 }),
role: mysqlEnum("role", ["user", "admin"]).default("user").notNull(),
createdAt: timestamp("createdAt").defaultNow().notNull(),
updatedAt: timestamp("updatedAt").defaultNow().onUpdateNow().notNull(),
lastSignedIn: timestamp("lastSignedIn").defaultNow().notNull(),
});
External Service Integrations
The backend acts as a proxy to external services for complex tasks like AI-powered chat and file storage, using the "Forge" API.
LLM Service (Forge API)
The invokeLLM function provides an interface to a large language model. It sends a structured payload to the Forge chat completions API endpoint.
- Endpoint:
https://forge.manus.im/v1/chat/completions(or a configured URL). - Authentication: Uses a
Bearertoken from theFORGE_API_KEYenvironment variable. - Functionality: Supports multi-modal messages (text, image, file), tool calls, and structured JSON output.
The flow for invoking the LLM is as follows:
Sources: server/_core/llm.ts:223-274
The Message type is a core data structure for LLM interaction.
| Field | Type | Description |
|---|---|---|
role | system, user, assistant, etc. | The role of the message author. |
content | string or MessageContent[] | The content of the message (text, image, file). |
name | string (optional) | The name of the tool call author. |
Sources: server/_core/llm.ts:21-27
File Storage (Forge API)
The server/storage.ts module provides helpers to interact with a storage proxy, also part of the Forge API.
storagePut(relKey, data, contentType): Uploads a file. It constructs aFormDataobject and POSTs it to the/v1/storage/uploadendpoint.storageGet(relKey): Retrieves a temporary, signed download URL for a file by calling the/v1/storage/downloadUrlendpoint.
Authentication for these operations also relies on the FORGE_API_KEY.
This diagram shows the file upload process.
Sources: server/storage.ts:63-83
Summary
The backend architecture of the aidoit4u application is a modern, service-oriented system built on Node.js. It effectively decouples core business logic from external dependencies by acting as a gateway. Authentication is robustly handled through Manus OAuth and JWTs, while data persistence is managed by the type-safe Drizzle ORM. By integrating with powerful external APIs for LLM and storage, the backend provides a scalable and feature-rich foundation for the client applications.