Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .env.example

This file was deleted.

26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI

on:
push:
branches: [main]
pull_request:

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm run typecheck
- run: npm run lint
- run: npm test
- run: npm run build
env:
# Placeholders: nothing is fetched from Supabase at build time, but the
# client factories read these at module load.
NEXT_PUBLIC_SUPABASE_URL: https://placeholder.supabase.co
NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY: sb_publishable_placeholder
69 changes: 33 additions & 36 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,44 +1,41 @@
# Node
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
dist/
.eslintcache
/.pnp
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/versions

# Env files
.env
server/.env
# testing
/coverage

# Logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
*.log
# next.js
/.next/
/out/

# production
/build

# Editor
.vscode/
# misc
.DS_Store
# Logs
logs
*.log
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

# server/.gitignore
node_modules
.env
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
.pnpm-debug.log*

# env files (can opt-in for committing if needed)
.env*

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
9 changes: 9 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- BEGIN:nextjs-agent-rules -->

# This is NOT the Next.js you know

This version has breaking changes — APIs, conventions, and file structure may all differ from your training data. Read the relevant guide in `node_modules/next/dist/docs/` (resolved from this file's directory; in monorepos the `next` package may not be visible from the repo root) before writing any code. Heed deprecation notices.

This block is written and re-added by `next dev` — verify at `node_modules/next/dist/server/lib/generate-agent-files.js`. Removing it from a diff only re-creates the uncommitted change; committing it with your work keeps the tree clean.

<!-- END:nextjs-agent-rules -->
1 change: 1 addition & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@AGENTS.md
240 changes: 107 additions & 133 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,159 +1,133 @@
# PixelChat

Modern chat application with React + Vite (frontend) and Express + Socket.IO (backend).

## Local development

1. Frontend env: create `.env` in project root

```
VITE_API_BASE_URL=http://localhost:5000
```

2. Backend env: create `server/.env`

```
PORT=5000
CLIENT_ORIGIN=http://localhost:5173
MONGO_URI=your_mongodb_uri
JWT_SECRET=change-me
CLOUDINARY_CLOUD_NAME=
CLOUDINARY_API_KEY=
CLOUDINARY_API_SECRET=
Real-time 1:1 chat — friend requests, presence, typing indicators, and file
attachments. Built with Next.js (App Router) and Supabase.

## Stack

| Concern | Choice |
| --- | --- |
| App | Next.js 16 App Router, React 19, TypeScript, Tailwind v4, shadcn/ui |
| Auth | Supabase Auth via `@supabase/ssr` — httpOnly cookie sessions |
| Data | Supabase Postgres, access controlled entirely by Row Level Security |
| Real-time | Supabase Realtime — private Broadcast channels and Presence |
| Files | Supabase Storage, private bucket with signed URLs |
| Errors | Sentry (`@sentry/nextjs`) |
| Hosting | Vercel |

## How access control works

There is no API server. The browser talks to Postgres through PostgREST, and
every rule is enforced in the database, so a request that skips the UI is
subject to the same checks:

- **Membership** is the single predicate. `private.is_member(conversation_id)`
answers only *"is the caller a member"*, so it cannot leak anything about
anyone else, and every policy on messages, conversations, and members is built
on it.
- **Sender identity is pinned.** The insert policy on `messages` requires
`sender_id = auth.uid()`, so a message cannot be attributed to someone else.
- **Messages are immutable.** `authenticated` holds only `SELECT, INSERT` on
`messages` — no update or delete privilege exists to abuse.
- **Realtime is authorized too.** Channels are opened with `private: true` and
checked against RLS policies on `realtime.messages`, so joining
`conversation:<id>` you are not a member of is refused at the socket.
- **Notifications cannot be forged.** Clients may read `user:<their own id>` but
never write to it; only a definer-rights trigger publishes there.
- **Attachments are private.** Object paths are `<conversation_id>/<file>` and
storage policies re-check membership when a signed URL is minted.
- **`anon` has no table privileges at all.** Signed out, nothing is readable.

`supabase/tests/rls_test.sql` asserts all of this. It seeds its own users, runs
the checks, and rolls back:

```bash
supabase db execute --file supabase/tests/rls_test.sql
```

3. Install and run
## Local development

```
```bash
npm install
npm --prefix server install
cp .env.example .env.local # fill in from Supabase → Project Settings → API
npm run dev
```

## Deployment

### Frontend (Vercel)

- Set Project Settings → Environment Variables:
- `VITE_API_BASE_URL` → https://your-backend.onrender.com
- Deploy. `vercel.json` routes SPA to `index.html`.
`.env.local`:

### Backend (Render)

- Create a Web Service from `server/`
- Use Node version 22 (or LTS compatible)
- Build command: `npm install`
- Start command: `node src/server.js`
- Environment Variables:
- `PORT` → Render provides a port, but we default to 10000 in `render.yaml` (Render sets PORT at runtime; our server respects `process.env.PORT`).
- `CLIENT_ORIGIN` → https://your-frontend.vercel.app
- `MONGO_URI`, `JWT_SECRET`, `CLOUDINARY_*` as required

## Notes

- The client and Socket.IO use `VITE_API_BASE_URL` for API and socket connection.
- The server allows CORS for `CLIENT_ORIGIN`. In dev it also allows localhost:5173/5174.
- Real-time events: client joins a room named by the user’s ID on connect.
# PixelChat: Real-Time Chat Application

<img width="960" height="540" alt="image" src="https://github.com/user-attachments/assets/b2d6b944-e1da-4cea-bcb2-168bbbb5dae0" />


PixelChat is a modern, full-stack, real-time chat application built with React, TypeScript, and Node.js. It features a clean, responsive UI inspired by platforms like Discord and WhatsApp, and provides a complete, feature-rich communication experience.

---
## ✨ Features
```
NEXT_PUBLIC_SUPABASE_URL=https://<project-ref>.supabase.co
NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY=sb_publishable_...
NEXT_PUBLIC_SENTRY_DSN=https://...ingest.de.sentry.io/...
```

* **Secure User Authentication:** Full user registration and login functionality using JWT (JSON Web Tokens) for secure, session-based authentication.
* **Real-Time One-on-One & Group Chats:** Instant messaging in both private and group chat rooms, powered by a robust WebSocket implementation with Socket.IO.
* **User Presence Indicators:** See which users are currently "online" or "offline" and get real-time "is typing..." feedback during conversations.
* **File & Image Sharing:** Seamlessly upload and share images and documents within any chat, with files stored securely in the cloud.
* **End-to-End Encryption (E2EE):** A robust security model ensuring that only the sender and intended recipients can read message contents.
* **User Profiles & Settings:** A dedicated page for users to view and update their profile information, including their username and avatar.
* **Modern UI:** A sleek and responsive user interface built with **Vite**, **React**, **TypeScript**, and styled with **Shadcn UI** & **Tailwind CSS**.
* **Scalable Backend:** A powerful backend built with **Node.js**, **Express**, and **TypeScript** for type safety and maintainability.
Both Supabase values are meant to be public. The service role key is not used
anywhere in this project and must never be added to a `NEXT_PUBLIC_` variable.

---
## 💻 Tech Stack
Checks:

This project is built with a modern, full-stack technology set.
```bash
npm run typecheck
npm run lint
npm run build
```

### Frontend
| Technology | Description |
| :--- | :--- |
| **Vite** | Next-generation frontend tooling for a fast development experience. |
| **React** | A JavaScript library for building user interfaces. |
| **TypeScript**| A typed superset of JavaScript that compiles to plain JavaScript. |
| **Shadcn UI** | A collection of beautifully designed, accessible UI components. |
| **Tailwind CSS** | A utility-first CSS framework for rapid UI development. |
| **Socket.IO Client**| Real-time, bidirectional event-based communication. |
| **Axios** | A promise-based HTTP client for making API requests. |
## Database changes

### Backend
| Technology | Description |
| :--- | :--- |
| **Node.js** | A JavaScript runtime built on Chrome's V8 JavaScript engine. |
| **Express.js** | A minimal and flexible Node.js web application framework. |
| **TypeScript**| Adds static typing to enhance code quality and maintainability. |
| **MongoDB** | A NoSQL database for storing user and message data. |
| **Mongoose** | An elegant MongoDB object modeling tool for Node.js. |
| **Socket.IO** | Enables real-time, bidirectional communication between clients and server.|
| **JWT** | JSON Web Tokens for secure user authentication. |
| **Bcrypt.js** | A library for hashing passwords securely. |
Migrations live in `supabase/migrations/` and are the source of truth.

---
## 🏛️ Architectural Design: Object-Oriented Principles
```bash
supabase link --project-ref <project-ref>
supabase db push
```

The backend is architected using key Object-Oriented Programming (OOP) principles to ensure the system is scalable, modular, and maintainable.
After changing the schema, regenerate types and re-run the linter:

* **Encapsulation:** Core entities like `User`, `Message`, and `ChatRoom` are modeled as classes, encapsulating their data (properties) and behavior (methods) to protect their internal state and maintain a clear API.
* **Inheritance:** A base `Message` class is extended by specialized classes like `TextMessage` and `FileMessage`, promoting code reuse and a logical class hierarchy.
* **Polymorphism:** The application can process various message types through a common interface, allowing for flexible and extensible handling of different kinds of chat content.
```bash
supabase gen types typescript --project-id <project-ref> > src/lib/supabase/database.types.ts
supabase db advisors
```

---
## ⚙️ Getting Started
New tables need `ALTER TABLE ... ENABLE ROW LEVEL SECURITY`, policies, and an
explicit `GRANT` — Supabase no longer exposes new public tables to the Data API
automatically, and its default privileges hand `ALL` to `anon` and
`authenticated` until revoked (see
`supabase/migrations/20260825132353_tighten_table_privileges.sql`).

To get a local copy up and running, follow these simple steps.
## Deployment

### Prerequisites
* Node.js (LTS version) installed
* npm (comes with Node.js)
* A MongoDB Atlas account or a local MongoDB instance
**Vercel** — import the repo and set, for Production and Preview:

### Installation & Setup
- `NEXT_PUBLIC_SUPABASE_URL`
- `NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY`
- `NEXT_PUBLIC_SENTRY_DSN`
- `SENTRY_AUTH_TOKEN` — build-time only; without it stack traces stay minified

1. **Clone the repository:**
```sh
git clone [https://github.com/your-username/pixelchat.git](https://github.com/your-username/pixelchat.git)
cd pixelchat
```
**Supabase** — under Authentication → URL Configuration add the production URL
as the Site URL and `https://<project>-*.vercel.app/**` as a redirect URL so
preview deployments can complete sign-in.

2. **Set up the Backend:**
```sh
# Navigate to the server directory
cd server

# Install backend dependencies
npm install

# Create a .env file in the /server directory and add your variables
# MONGO_URI=your_mongodb_connection_string
# JWT_SECRET=your_jwt_secret

# Start the backend server
npm run dev
```

3. **Set up the Frontend:**
```sh
# Open a new terminal and navigate to the project's root directory
cd ..
Email confirmation uses Supabase's built-in SMTP, which is rate limited to a
handful of messages per hour and is not suitable for production. Configure a
real SMTP provider before launch, or turn confirmation off deliberately.

# Install frontend dependencies
npm install

# Start the frontend development server
npm run dev
```
## Project layout

Your application should now be running, with the frontend available at `http://localhost:5173` and the backend at `http://localhost:5000`.
```
src/
app/
auth/ server actions for sign in/up/out, and the callback route
chat/ the authenticated chat page (server-rendered shell)
login, signup auth pages
components/
chat/ chat shell, thread, attachments, friends
ui/ shadcn primitives
lib/
supabase/ browser and server client factories, generated types
validation.ts zod schemas shared by forms and actions
proxy.ts session refresh and route gating (Next 16 renamed
middleware.ts to proxy.ts)
supabase/
migrations/ schema, RLS policies, triggers, functions
tests/rls_test.sql security regression test
```
Loading
Loading