A secure, end-to-end encrypted real-time chat application built with NestJS.
- End-to-End Encryption - All messages are encrypted using libsodium's XChaCha20-Poly1305 encryption
- Real-time Communication - Uses WebSockets (Socket.io) for instant messaging
- User Authentication - Email/password, Google and GitHub OAuth support
- Password Reset - Secure password reset flow with email notifications
- Message History - Persistent chat history stored securely
- Message Editing - Edit previously sent messages
- Typing Indicators - Real-time typing notifications
- Online Status - See when users are online/offline
- Rate Limiting - Protection against brute force attacks
- Security Headers - Implemented security best practices
- Backend - NestJS with TypeScript
- Database - PostgreSQL with TypeORM
- Authentication - JWT with Passport
- Real-time - Socket.io
- Encryption - libsodium-wrappers
- Email - Nodemailer with Gmail SMTP
- Node.js (v16+)
- npm or pnpm
- PostgreSQL database
- Clone the repository
- Install dependencies:
$ pnpm install
- Create a
.env
file in the root directory with the following variables:
DB_URL=your_postgres_connection_string
FRONTEND_URL=http://localhost:3000
PORT=8000
JWT_SECRET=your_jwt_secret
JWT_REFRESH_SECRET=your_refresh_token_secret
# For Google OAuth (optional)
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
GOOGLE_CALLBACK_URL=http://localhost:8000/api/auth/google/callback
# For GitHub OAuth (optional)
GITHUB_CLIENT_ID=your_github_client_id
GITHUB_CLIENT_SECRET=your_github_client_secret
GITHUB_CALLBACK_URL=http://localhost:8000/api/auth/github/callback
# For email functionality
EMAIL_USER=your_email_address
EMAIL_PASSWORD=your_email_password
# development mode
$ pnpm run start:dev
$ pnpm run start:debug
# production mode
$ pnpm run build
$ pnpm run start:prod
This project uses TypeORM for database management. You can run migrations with the following commands:
# Generate a migration
$ pnpm run migration:generate -- src/migrations/MigrationName
# Run migrations
$ pnpm run migration:run
# Revert migrations
$ pnpm run migration:revert
# Show migration status
$ pnpm run migration:show
POST /auth/register
- Register a new userPOST /auth/login
- Login with email and passwordPOST /auth/refresh
- Refresh access tokenPOST /auth/forgot-password
- Request password resetPOST /auth/reset-password
- Reset password with tokenGET /auth/verify
- Verify current user (requires JWT)GET /auth/verify-email
- Verify email with tokenGET /auth/google
- Google OAuth loginGET /auth/google/callback
- Google OAuth callbackGET /auth/github
- GitHub OAuth loginGET /auth/github/callback
- GitHub OAuth callbackPOST /auth/public-key
- Update user's public key (requires JWT)
POST /users
- Create a new userGET /users
- Get all usersGET /users/me
- Get current user profileGET /users/search
- Search for users by usernameGET /users/:username
- Get user by usernameGET /users/:id
- Get user by IDPATCH /users/:id
- Update user (requires user ownership)DELETE /users/:id
- Delete user (requires user ownership)
GET /chat/conversation/:otherUserId
- Get conversation with a specific userGET /chat/recent
- Get recent chatsGET /chat/search
- Search messages (query params: query, otherUserId)GET /chat/recent-users
- Get users with recent chatsDELETE /chat/:id
- Delete a messagePUT /chat/mark-read/:recipientId
- Mark messages as read
sendMessage
- Send a new messageeditMessage
- Edit a messagegetConversation
- Get conversation historytyping
- Indicate user is typing
newMessage
- Receive a new messagemessageEdited
- Message has been editeduserTyping
- User is typinguserConnected
- User came onlineuserDisconnected
- User went offline
# unit tests
$ pnpm run test
# e2e tests
$ pnpm run test:e2e
# test coverage
$ pnpm run test:cov
- All messages are end-to-end encrypted
- Passwords are hashed using bcrypt
- Rate limiting is applied to authentication endpoints
- JSON Web Tokens are used for authentication
- Security headers are implemented
- CORS is configured appropriately
- Environment variables are used for sensitive information
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request