RJRYT/NcrpUcp
SA-MP New City RP User Control Panel with Discord OAuth2, built with Node.js, Express, React & Vite
README.md
SampUcpNcrp
User Control Panel (UCP) for the New City RP SA-MP server.
A full-stack web panel built with Node.js, Express, React, and Vite. Provides user authentication via Discord OAuth2, profile and vehicle management, premium VIP tiers, admin tooling, and a Discord-bot-connected backend.
Table of Contents
- Features
- Tech Stack
- Project Structure
- Prerequisites
- Installation
- Environment Variables
- Database Setup
- Running the Application
- Building for Production
- Available Scripts
- Authentication & Authorization
- API Routes Overview
- SA-MP Integration
- Logging
- Testing
- Contributing
- License
- Author
Features
- Discord OAuth2 authentication via
passport-discord - Role-based permission system stored in MongoDB (
admin,moderator,user) - Route-level permission guards with Express middleware
- User profiles, vehicles, inventory, factions, and gangs management
- Premium VIP tiers (Bronze, Silver, Gold, Platinum) with purchase pages
- Song uploads and management with
multer - Web push notifications
- FAQ system
- Admin console with user management, web logs, and statistics
- Discord bot integration (
discord.js) with a globalDiscordClient - SA-MP static file hosting (APK downloads, client files)
- Scheduled alert jobs via
node-cron(daily spike and negative-balance checks) - Secure defaults: Helmet CSP, CORS, express-rate-limit, express-validator
- Structured logging with Winston daily rotation
- Frontend: React 18, Vite, Tailwind CSS, React Router, Axios, Framer Motion, React Toastify, Chart.js
Tech Stack
Backend
| Layer | Technology |
|---|---|
| Runtime | Node.js 18+ |
| Framework | Express 4 |
| Database (users/game data) | MySQL 8 (mysql2) |
| Database (permissions/roles) | MongoDB (mongoose) |
| Authentication | passport, passport-discord, jsonwebtoken |
| Security | helmet, cors, express-rate-limit, express-validator |
| File uploads | multer |
| Logging | winston, winston-daily-rotate-file |
| Scheduling | node-cron |
| Discord | discord.js |
| Dev tooling | nodemon |
Frontend
| Layer | Technology |
|---|---|
| Build tool | Vite 6 |
| UI library | React 18 |
| Styling | Tailwind CSS |
| Routing | React Router DOM 7 |
| HTTP client | Axios |
| Animations | Framer Motion |
| Notifications | React Toastify |
| Charts | Chart.js / react-chartjs-2 |
| Icons | Lucide React, React Icons |
| 3D models | @google/model-viewer |
Project Structure
samp-ucp/
├── main.js # Express backend entry point
├── socketStore.js # SA-MP TCP socket state store
├── initPermission.js # MongoDB permission/role seeder
├── .env # Environment variables (gitignored)
├── .env.example # Example environment template
├── package.json # Backend dependencies & scripts
├── package-lock.json
├── .gitignore
├── backup.md
├── routes.log
├── src/
│ ├── config/ # db.js, helmetConfig.js, permissions.js, rateLimit.js
│ ├── controllers/ # Route handlers (auth, user, admin, premium, songs, etc.)
│ ├── middleware/ # authMiddleware, errorHandler, requestLogger, accessLogger, restrictToPermission
│ ├── routes/ # Express route definitions (/api/auth, /api/user, /api/admin, ...)
│ ├── services/ # Business logic (userService, transactionService, alertService, ...)
│ ├── utils/ # hashPassword, generateToken, loggers, syncPermissions, ipUtils
│ └── bot/ # Discord bot client and API routes
├── client/ # Vite + React frontend
│ ├── src/
│ │ ├── components/ # Reusable UI components (Navbar, ProtectedRoute, PermCheck, ...)
│ │ ├── context/ # AuthContext
│ │ ├── pages/ # Page components (UserHome, LoginPage, AdminPanel, ...)
│ │ ├── services/ # api.js (Axios instance)
│ │ └── utils/ # handleApiError, constants, debugLogger
│ ├── vite.config.js
│ ├── tailwind.config.js
│ └── package.json # Frontend dependencies & scripts
├── samp/ # Static SA-MP files served by Express (APK, client files)
├── test/ # Placeholder test files (socket.js, client.js)
└── logs/ # Winston daily-rotated logs (server, request, error, console)
Prerequisites
Verify the following are installed on your machine:
| Tool | Purpose | Verify command |
|---|---|---|
| Node.js (18+) | JavaScript runtime | node -v |
| npm | Package manager | npm -v |
| MySQL 8+ | Primary application database | mysql --version |
| MongoDB 4+ | Permissions, roles, route-permissions | mongod --version |
| Discord application | OAuth2 credentials | Discord Developer Portal |
Installation
1. Clone the repository
git clone https://github.com/RJRYT/NcrpUcp.git
cd NcrpUcp
2. Install backend dependencies
npm install
3. Install frontend dependencies
cd client
npm install
cd ..
4. Configure environment variables
Create a .env file in the project root. The file is gitignored, so it will not be committed.
See the Environment Variables section below for the full list of required variables.
5. Seed the permission system
Run the MongoDB seeder to initialize permissions, roles, and route-permissions:
node initPermission.js
⚠️ This script clears and repopulates the
Permission,Role, andRoutePermissioncollections. Only run it on a fresh or disposable database.
Environment Variables
Create .env at the project root with the following variables:
| Variable | Example | Description |
|---|---|---|
APPLICATION_PORT | 3000 | Backend server port |
MYSQL_DATABASE_HOST | localhost | MySQL host |
MYSQL_DATABASE_USER | root | MySQL username |
MYSQL_DATABASE_PASSWORD | password | MySQL password |
MYSQL_DATABASE_NAME | samp_ucp | MySQL database name |
MONGO_URI | mongodb://localhost:27017/samp_ucp | MongoDB connection string |
DISCORD_0AUTH2_CLIENT_ID | 1234567890... | Discord OAuth2 client ID |
DISCORD_0AUTH2_CLIENT_SECRET | abcdef123456... | Discord OAuth2 client secret |
DISCORD_0AUTH2_CALLBACK_URL | http://localhost:3000/api/auth/discord/callback | Discord OAuth2 redirect URI |
API_DEBUG_LOG | true | Enable MySQL pool debug logging |
Keep
.envout of version control. Never commit real secrets.
Database Setup
MySQL
Create the database and user referenced in your .env, then run your schema migrations against it. The application expects tables for users, vehicles, factions, gangs, inventory, transactions, and related game data (referenced across src/routes/ and src/services/).
MongoDB
Ensure MongoDB is running, then execute the seeder:
node initPermission.js
This creates default roles (admin, moderator, user) and maps them to permissions and protected routes.
Running the Application
Backend (development)
# With auto-restart via nodemon
npm run dev
# Or plain Node
npm start
The backend starts on http://localhost:3000. It:
- Connects the Discord bot client (exposed as
global.DiscordClient) - Serves the production frontend from
client/dist/ - Serves static SA-MP files from
samp/ - Mounts all
/api/*routes - Runs a daily cron job at 00:05 for alert checks
Frontend (development)
cd client
npm run dev # Vite dev server (typically http://localhost:5173)
Running both together
You can run the backend and frontend in separate terminals, or use a process manager such as concurrently if you add it to your scripts.
Building for Production
1. Build the frontend
cd client
npm run build # Outputs to client/dist/
cd ..
2. Start the backend
npm start
Express serves client/dist/ statically, so the built frontend is served by the same Node process that runs the API.
3. Preview the frontend build locally (optional)
cd client
npm run preview
Available Scripts
Root (package.json)
| Script | Command | Description |
|---|---|---|
dev | nodemon . | Start backend with auto-restart |
start | node . | Start backend in production |
test | echo "Error: no test specified" && exit 1 | No test suite configured |
Client (client/package.json)
| Script | Command | Description |
|---|---|---|
dev | vite | Start frontend dev server |
build | vite build | Build frontend for production |
lint | eslint . | Lint frontend source |
preview | vite preview | Preview production build locally |
Authentication & Authorization
Authentication flow
- User selects Login with Discord.
- Browser redirects to Discord OAuth2.
- Discord redirects back to the callback route handled by
passport-discord. - On success, the backend issues a session/token (see
src/controllers/authController.js). - The frontend stores the token and attaches it to outgoing Axios requests via
client/src/services/api.js.
Authorization
src/middleware/authMiddleware.jsprotects API routes.src/middleware/restrictToPermission.jsenforces MongoDB-based route permissions.- Permissions, roles, and route-permissions are seeded by
initPermission.jsand stored in MongoDB.
Default roles:
- admin — Full access to all routes
- moderator — Access to most routes, excluding permission management
- user — Basic access to personal and public data
API Routes Overview
| Prefix | Controller | Purpose |
|---|---|---|
/api/auth | authController | Discord login, registration, callbacks |
/api/user | userController | Profile, stats, inventory, vehicles, factions, gangs |
/api/premium | premiumController | VIP tiers and purchases |
/api/admin | adminController | Admin console, user management, logs |
/api/perms | permissionController | Permission, role, and route-permission CRUD |
/api/push | — | Web push notifications |
/api/faq | — | FAQ management |
/api/songs | songsController | Song uploads and management |
/api | src/bot/api.js | Bot-related API endpoints |
SA-MP Integration
- Express serves static files from the
samp/directory (e.g., APK downloads for the SA-MP client). - A TCP server for direct SA-MP communication is defined in
main.jsbut is currently commented out. Uncomment and configure theTCP_PORT/TCP_HOSTblock if you need live game-server socket integration. socketStore.jstracks the connected SA-MP socket state.global.DiscordClientexposes the Discord bot instance across the Express app.
Logging
Winston with daily rotation writes to:
| Directory | Contents |
|---|---|
logs/server/ | Server access logs |
logs/request/ | Categorized request logs |
logs/error/ | Application errors, exceptions, and rejections |
logs/console/ | Console output logs |
Log directories are gitignored and generated at runtime.
Testing
There is no test suite configured in this repository. The root package.json defines test as a placeholder that exits with an error.
If you wish to add testing, consider installing vitest or jest + supertest and writing tests for:
- Auth controllers and middleware
- Express route validation (
express-validatoris already a dependency) - Service-layer business logic
Placeholder files exist under test/ (socket.js, client.js) but are not wired to a test runner.
Contributing
- Fork the repository.
- Create a feature branch (
git checkout -b feature/your-feature). - Commit your changes (
git commit -m "feat: add your feature"). - Push to the branch (
git push origin feature/your-feature). - Open a Pull Request.
Please follow the existing code style and keep changes focused and minimal.
License
This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0). See the LICENSE file for details.
Author
RJRYT — New City RP User Control Panel
discord-botdiscord-oauth2expressgamingmongodbmysqlnodejsreactroleplayroleplay-serversampsan-andreas-multiplayerucpuser-control-panelvite
Diese Seite wird aus dem erstellt, was GitHub über das Repository veröffentlicht. samp.directory hostet den Code nicht. Ein Problem mit diesem Eintrag melden