backend
This commit is contained in:
parent
c93d9f872c
commit
99cbd6e310
7
backend/.gitignore
vendored
Normal file
7
backend/.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
node_modules
|
||||||
|
|
||||||
|
.env
|
||||||
|
|
||||||
|
/generated/prisma
|
||||||
|
|
||||||
|
/generated/prisma
|
||||||
1
backend/.prettierignore
Normal file
1
backend/.prettierignore
Normal file
@ -0,0 +1 @@
|
|||||||
|
dist
|
||||||
3
backend/.prettierrc
Normal file
3
backend/.prettierrc
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"printWidth": 150
|
||||||
|
}
|
||||||
3
backend/.vscode/extensions.json
vendored
Normal file
3
backend/.vscode/extensions.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"recommendations": ["dbaeumer.vscode-eslint", "esbenp.prettier-vscode", "ms-vscode.vscode-typescript-next"]
|
||||||
|
}
|
||||||
10
backend/.vscode/settings.json
vendored
Normal file
10
backend/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"editor.codeActionsOnSave": {
|
||||||
|
"source.fixAll.eslint": true,
|
||||||
|
"source.organizeImports": true
|
||||||
|
},
|
||||||
|
"editor.formatOnSave": true,
|
||||||
|
"eslint.validate": ["javascript", "typescript", "typescriptreact"],
|
||||||
|
"eslint.alwaysShowStatus": true,
|
||||||
|
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
||||||
|
}
|
||||||
22
backend/dist/config/database.js
vendored
Normal file
22
backend/dist/config/database.js
vendored
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.disconnectDatabase = exports.connectDatabase = exports.prisma = void 0;
|
||||||
|
const client_1 = require("@prisma/client");
|
||||||
|
exports.prisma = new client_1.PrismaClient({
|
||||||
|
log: ["query", "info", "warn", "error"],
|
||||||
|
});
|
||||||
|
const connectDatabase = async () => {
|
||||||
|
try {
|
||||||
|
await exports.prisma.$connect();
|
||||||
|
console.log("✅ Database connected successfully");
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error("❌ Database connection failed:", error);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
exports.connectDatabase = connectDatabase;
|
||||||
|
const disconnectDatabase = async () => {
|
||||||
|
await exports.prisma.$disconnect();
|
||||||
|
};
|
||||||
|
exports.disconnectDatabase = disconnectDatabase;
|
||||||
12
backend/dist/config/env.js
vendored
Normal file
12
backend/dist/config/env.js
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.env = void 0;
|
||||||
|
const zod_1 = require("zod");
|
||||||
|
const envSchema = zod_1.z.object({
|
||||||
|
CORS_ORIGIN: zod_1.z.string().default("*"),
|
||||||
|
DATABASE_URL: zod_1.z.string(),
|
||||||
|
JWT_SECRET: zod_1.z.string(),
|
||||||
|
NODE_ENV: zod_1.z.enum(["development", "production", "test"]).default("development"),
|
||||||
|
PORT: zod_1.z.string().transform(Number).default("3000"),
|
||||||
|
});
|
||||||
|
exports.env = envSchema.parse(process.env);
|
||||||
17
backend/dist/controllers/authController.js
vendored
Normal file
17
backend/dist/controllers/authController.js
vendored
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.AuthController = void 0;
|
||||||
|
const authService_js_1 = require("../services/authService.js");
|
||||||
|
class AuthController {
|
||||||
|
static async register(req, res) {
|
||||||
|
const data = req.body;
|
||||||
|
const result = await authService_js_1.AuthService.register(data);
|
||||||
|
res.status(result.success ? 201 : 400).json(result);
|
||||||
|
}
|
||||||
|
static async login(req, res) {
|
||||||
|
const data = req.body;
|
||||||
|
const result = await authService_js_1.AuthService.login(data);
|
||||||
|
res.status(result.success ? 200 : 400).json(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.AuthController = AuthController;
|
||||||
37
backend/dist/controllers/chatController.js
vendored
Normal file
37
backend/dist/controllers/chatController.js
vendored
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.ChatController = void 0;
|
||||||
|
const chatService_js_1 = require("../services/chatService.js");
|
||||||
|
class ChatController {
|
||||||
|
static async getChatRooms(req, res) {
|
||||||
|
if (!req.user) {
|
||||||
|
res.status(401).json({ success: false, error: "Unauthorized" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const result = await chatService_js_1.ChatService.getChatRooms(req.user.userId);
|
||||||
|
res.status(result.success ? 200 : 500).json(result);
|
||||||
|
}
|
||||||
|
static async createChatRoom(req, res) {
|
||||||
|
if (!req.user) {
|
||||||
|
res.status(401).json({ success: false, error: "Unauthorized" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const data = req.body;
|
||||||
|
const result = await chatService_js_1.ChatService.createChatRoom(req.user.userId, data);
|
||||||
|
res.status(result.success ? 201 : 500).json(result);
|
||||||
|
}
|
||||||
|
static async getMessages(req, res) {
|
||||||
|
if (!req.user) {
|
||||||
|
res.status(401).json({ success: false, error: "Unauthorized" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const query = {
|
||||||
|
roomId: req.params.roomId,
|
||||||
|
page: req.query.page,
|
||||||
|
limit: req.query.limit,
|
||||||
|
};
|
||||||
|
const result = await chatService_js_1.ChatService.getMessages(query);
|
||||||
|
res.status(result.success ? 200 : 500).json(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.ChatController = ChatController;
|
||||||
64
backend/dist/index.js
vendored
Normal file
64
backend/dist/index.js
vendored
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
"use strict";
|
||||||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||||
|
};
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
const cors_1 = __importDefault(require("cors"));
|
||||||
|
const express_1 = __importDefault(require("express"));
|
||||||
|
const http_1 = __importDefault(require("http"));
|
||||||
|
const socket_io_1 = require("socket.io");
|
||||||
|
const database_js_1 = require("./config/database.js");
|
||||||
|
const env_js_1 = require("./config/env.js");
|
||||||
|
const auth_js_1 = require("./middleware/auth.js");
|
||||||
|
const socketHandlers_js_1 = require("./socket/socketHandlers.js");
|
||||||
|
const app = (0, express_1.default)();
|
||||||
|
const server = http_1.default.createServer(app);
|
||||||
|
const io = new socket_io_1.Server(server, {
|
||||||
|
cors: {
|
||||||
|
methods: ["GET", "POST"],
|
||||||
|
origin: "http://localhost:5173",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
app.use((0, cors_1.default)({
|
||||||
|
methods: ["GET", "POST"],
|
||||||
|
origin: "http://localhost:5173",
|
||||||
|
}));
|
||||||
|
app.use(express_1.default.json());
|
||||||
|
app.get("/health", (_, res) => {
|
||||||
|
res.json({ status: "OK", timestamp: new Date().toISOString() });
|
||||||
|
});
|
||||||
|
io.use(auth_js_1.authenticateSocket);
|
||||||
|
io.on("connection", async (socket) => {
|
||||||
|
await (0, socketHandlers_js_1.handleConnection)(io, socket);
|
||||||
|
});
|
||||||
|
app.use((err, _, res) => {
|
||||||
|
console.error("Unhandled error:", err);
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
error: env_js_1.env.NODE_ENV === "production" ? "Internal server error" : err.message,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
const startServer = async () => {
|
||||||
|
try {
|
||||||
|
await (0, database_js_1.connectDatabase)();
|
||||||
|
server.listen(env_js_1.env.PORT, () => {
|
||||||
|
console.log(`🚀 Server running on port ${env_js_1.env.PORT}`);
|
||||||
|
console.log(`📊 Environment: ${env_js_1.env.NODE_ENV}`);
|
||||||
|
console.log(`🔗 CORS Origin: ${env_js_1.env.CORS_ORIGIN}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error("Failed to start server:", error);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
process.on("SIGTERM", () => {
|
||||||
|
console.log("SIGTERM received, shutting down gracefully...");
|
||||||
|
server.close(() => {
|
||||||
|
console.log("HTTP server closed");
|
||||||
|
process.exit(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
(async () => {
|
||||||
|
await startServer();
|
||||||
|
})();
|
||||||
51
backend/dist/middleware/auth.js
vendored
Normal file
51
backend/dist/middleware/auth.js
vendored
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
"use strict";
|
||||||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||||
|
};
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.authenticateSocket = exports.authenticateToken = void 0;
|
||||||
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
||||||
|
const database_js_1 = require("../config/database.js");
|
||||||
|
const env_js_1 = require("../config/env.js");
|
||||||
|
const authenticateToken = (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const authHeader = req.headers.authorization;
|
||||||
|
const token = authHeader?.split(" ")[1];
|
||||||
|
if (!token) {
|
||||||
|
res.status(401).json({ success: false, error: "Access token required" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const decoded = jsonwebtoken_1.default.verify(token, env_js_1.env.JWT_SECRET);
|
||||||
|
req.user = decoded;
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error("Authentication error:", error);
|
||||||
|
res.status(403).json({ success: false, error: "Invalid or expired token" });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
exports.authenticateToken = authenticateToken;
|
||||||
|
const authenticateSocket = async (socket, next) => {
|
||||||
|
try {
|
||||||
|
const token = socket.handshake.auth.token;
|
||||||
|
if (!token) {
|
||||||
|
return next(new Error("Authentication token required"));
|
||||||
|
}
|
||||||
|
const decoded = jsonwebtoken_1.default.verify(token, env_js_1.env.JWT_SECRET);
|
||||||
|
const user = await database_js_1.prisma.user.findUnique({
|
||||||
|
where: {
|
||||||
|
id: decoded.userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!user)
|
||||||
|
return next(new Error("User not found"));
|
||||||
|
const authenticateSocket = socket;
|
||||||
|
authenticateSocket.userId = user.id;
|
||||||
|
authenticateSocket.user = user;
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error("Socket authentication error:", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
exports.authenticateSocket = authenticateSocket;
|
||||||
42
backend/dist/middleware/rateLimiter.js
vendored
Normal file
42
backend/dist/middleware/rateLimiter.js
vendored
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.checkMessageRateLimit = exports.messageRateLimiter = void 0;
|
||||||
|
class RateLimiter {
|
||||||
|
limits = new Map();
|
||||||
|
maxMessages;
|
||||||
|
windowMs;
|
||||||
|
constructor(maxMessages = 10, windowMs = 60000) {
|
||||||
|
this.maxMessages = maxMessages;
|
||||||
|
this.windowMs = windowMs;
|
||||||
|
}
|
||||||
|
checkLimit(userId) {
|
||||||
|
const now = Date.now();
|
||||||
|
const userLimit = this.limits.get(userId) ?? { count: 0, resetTime: now + this.windowMs };
|
||||||
|
if (now > userLimit.resetTime) {
|
||||||
|
userLimit.count = 0;
|
||||||
|
userLimit.resetTime = now + this.windowMs;
|
||||||
|
}
|
||||||
|
userLimit.count++;
|
||||||
|
this.limits.set(userId, userLimit);
|
||||||
|
return userLimit.count <= this.maxMessages;
|
||||||
|
}
|
||||||
|
getRemainingTime(userId) {
|
||||||
|
const userLimit = this.limits.get(userId);
|
||||||
|
if (!userLimit)
|
||||||
|
return 0;
|
||||||
|
return Math.max(0, userLimit.resetTime - Date.now());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.messageRateLimiter = new RateLimiter(10, 60000); // 10 messages per minute
|
||||||
|
const checkMessageRateLimit = (socket) => {
|
||||||
|
const canSend = exports.messageRateLimiter.checkLimit(socket.userId);
|
||||||
|
if (!canSend) {
|
||||||
|
const remainingTime = exports.messageRateLimiter.getRemainingTime(socket.userId);
|
||||||
|
socket.emit("rate_limit_exceeded", {
|
||||||
|
message: `Too many messages. Please wait ${Math.ceil(remainingTime / 1000)} seconds before sending another message.`,
|
||||||
|
remainingTime,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return canSend;
|
||||||
|
};
|
||||||
|
exports.checkMessageRateLimit = checkMessageRateLimit;
|
||||||
38
backend/dist/middleware/validation.js
vendored
Normal file
38
backend/dist/middleware/validation.js
vendored
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.createChatRoomSchema = exports.registerSchema = exports.loginSchema = exports.validate = void 0;
|
||||||
|
const zod_1 = require("zod");
|
||||||
|
const validate = (schema) => {
|
||||||
|
return (req, res, next) => {
|
||||||
|
try {
|
||||||
|
schema.parse(req.body);
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
if (error instanceof zod_1.z.ZodError) {
|
||||||
|
res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
error: "Validation failed",
|
||||||
|
details: error.errors,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
exports.validate = validate;
|
||||||
|
exports.loginSchema = zod_1.z.object({
|
||||||
|
email: zod_1.z.string().email("Invalid email format"),
|
||||||
|
password: zod_1.z.string().min(6, "Password must be at least 6 characters long"),
|
||||||
|
});
|
||||||
|
exports.registerSchema = zod_1.z.object({
|
||||||
|
username: zod_1.z.string().min(3, "Username must be at least 3 characters long"),
|
||||||
|
email: zod_1.z.string().email("Invalid email format"),
|
||||||
|
password: zod_1.z.string().min(6, "Password must be at least 6 characters long"),
|
||||||
|
});
|
||||||
|
exports.createChatRoomSchema = zod_1.z.object({
|
||||||
|
name: zod_1.z.string().min(1, "Room name is required"),
|
||||||
|
description: zod_1.z.string().optional(),
|
||||||
|
memberUsernames: zod_1.z.array(zod_1.z.string()).default([]),
|
||||||
|
});
|
||||||
10
backend/dist/routes/authRoutes.js
vendored
Normal file
10
backend/dist/routes/authRoutes.js
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.authRoutes = void 0;
|
||||||
|
const authController_js_1 = require("../controllers/authController.js");
|
||||||
|
const validation_js_1 = require("../middleware/validation.js");
|
||||||
|
const express_1 = require("express");
|
||||||
|
const router = (0, express_1.Router)();
|
||||||
|
exports.authRoutes = router;
|
||||||
|
router.post("/register", (0, validation_js_1.validate)(validation_js_1.registerSchema), authController_js_1.AuthController.register);
|
||||||
|
router.post("/login", (0, validation_js_1.validate)(validation_js_1.loginSchema), authController_js_1.AuthController.login);
|
||||||
13
backend/dist/routes/chatRoutes.js
vendored
Normal file
13
backend/dist/routes/chatRoutes.js
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.chatRoutes = void 0;
|
||||||
|
const chatController_js_1 = require("../controllers/chatController.js");
|
||||||
|
const auth_js_1 = require("../middleware/auth.js");
|
||||||
|
const validation_js_1 = require("../middleware/validation.js");
|
||||||
|
const express_1 = require("express");
|
||||||
|
const router = (0, express_1.Router)();
|
||||||
|
exports.chatRoutes = router;
|
||||||
|
router.use(auth_js_1.authenticateToken);
|
||||||
|
router.get("/chat-rooms", chatController_js_1.ChatController.getChatRooms);
|
||||||
|
router.post("/chat-rooms", (0, validation_js_1.validate)(validation_js_1.createChatRoomSchema), chatController_js_1.ChatController.createChatRoom);
|
||||||
|
router.get("/messages/:roomId", chatController_js_1.ChatController.getMessages);
|
||||||
10
backend/dist/routes/index.js
vendored
Normal file
10
backend/dist/routes/index.js
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.apiRoutes = void 0;
|
||||||
|
const express_1 = require("express");
|
||||||
|
const authRoutes_js_1 = require("./authRoutes.js");
|
||||||
|
const chatRoutes_js_1 = require("./chatRoutes.js");
|
||||||
|
const router = (0, express_1.Router)();
|
||||||
|
exports.apiRoutes = router;
|
||||||
|
router.use("/auth", authRoutes_js_1.authRoutes);
|
||||||
|
router.use("/", chatRoutes_js_1.chatRoutes);
|
||||||
100
backend/dist/services/authService.js
vendored
Normal file
100
backend/dist/services/authService.js
vendored
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
"use strict";
|
||||||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||||
|
};
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.AuthService = void 0;
|
||||||
|
const database_js_1 = require("../config/database.js");
|
||||||
|
const env_js_1 = require("../config/env.js");
|
||||||
|
const bcryptjs_1 = __importDefault(require("bcryptjs"));
|
||||||
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
||||||
|
class AuthService {
|
||||||
|
static async register(data) {
|
||||||
|
try {
|
||||||
|
const existingUser = await database_js_1.prisma.user.findFirst({
|
||||||
|
where: {
|
||||||
|
OR: [{ email: data.email }, { username: data.username }],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (existingUser) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: existingUser.email === data.email ? "Email already exists" : "Username already exists",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
const hashedPassword = await bcryptjs_1.default.hash(data.password, 12);
|
||||||
|
const user = await database_js_1.prisma.user.create({
|
||||||
|
data: {
|
||||||
|
username: data.username,
|
||||||
|
email: data.email,
|
||||||
|
password: hashedPassword,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const token = this.generateToken({
|
||||||
|
userId: user.id,
|
||||||
|
username: user.username,
|
||||||
|
email: user.email,
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
data: {
|
||||||
|
token,
|
||||||
|
user: {
|
||||||
|
id: user.id,
|
||||||
|
username: user.username,
|
||||||
|
email: user.email,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error("Registration error:", error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: "Registration failed",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static async login(data) {
|
||||||
|
try {
|
||||||
|
const user = await database_js_1.prisma.user.findUnique({
|
||||||
|
where: {
|
||||||
|
email: data.email,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!user || !(await bcryptjs_1.default.compare(data.password, user.password))) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: "Invalid email or password",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
const token = this.generateToken({
|
||||||
|
userId: user.id,
|
||||||
|
username: user.username,
|
||||||
|
email: user.email,
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
data: {
|
||||||
|
token,
|
||||||
|
user: {
|
||||||
|
id: user.id,
|
||||||
|
username: user.username,
|
||||||
|
email: user.email,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error("Login error:", error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: "Login failed",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static generateToken(payload) {
|
||||||
|
return jsonwebtoken_1.default.sign(payload, env_js_1.env.JWT_SECRET, { expiresIn: "7d" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.AuthService = AuthService;
|
||||||
186
backend/dist/services/chatService.js
vendored
Normal file
186
backend/dist/services/chatService.js
vendored
Normal file
@ -0,0 +1,186 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.ChatService = void 0;
|
||||||
|
const database_js_1 = require("../config/database.js");
|
||||||
|
class ChatService {
|
||||||
|
static async getChatRooms(userId) {
|
||||||
|
try {
|
||||||
|
const chatRooms = await database_js_1.prisma.chatRoom.findMany({
|
||||||
|
where: {
|
||||||
|
members: {
|
||||||
|
some: {
|
||||||
|
userId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
members: {
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
username: true,
|
||||||
|
isOnline: true,
|
||||||
|
avatar: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
_count: {
|
||||||
|
select: {
|
||||||
|
messages: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
orderBy: {
|
||||||
|
updatedAt: "desc",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
data: chatRooms,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error("Get chat rooms error:", error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: "Failed to retrieve chat rooms",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static async createChatRoom(userId, data) {
|
||||||
|
try {
|
||||||
|
const chatRoom = await database_js_1.prisma.chatRoom.create({
|
||||||
|
data: {
|
||||||
|
name: data.name,
|
||||||
|
description: data.description,
|
||||||
|
createdBy: userId,
|
||||||
|
members: {
|
||||||
|
create: [
|
||||||
|
{
|
||||||
|
userId,
|
||||||
|
role: "admin",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (data.memberUsernames.length > 0) {
|
||||||
|
const users = await database_js_1.prisma.user.findMany({
|
||||||
|
where: {
|
||||||
|
username: {
|
||||||
|
in: data.memberUsernames,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const memberData = users.map((user) => ({
|
||||||
|
userId: user.id,
|
||||||
|
roomId: chatRoom.id,
|
||||||
|
}));
|
||||||
|
await database_js_1.prisma.chatRoomMember.createMany({
|
||||||
|
data: memberData,
|
||||||
|
skipDuplicates: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const fullChatRoom = await database_js_1.prisma.chatRoom.findUnique({
|
||||||
|
where: { id: chatRoom.id },
|
||||||
|
include: {
|
||||||
|
members: {
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
username: true,
|
||||||
|
isOnline: true,
|
||||||
|
avatar: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
_count: {
|
||||||
|
select: {
|
||||||
|
messages: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
data: fullChatRoom,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error("Create chat room error:", error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: "Failed to create chat room",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static async getMessages(query) {
|
||||||
|
try {
|
||||||
|
const page = Number.parseInt(query.page ?? "1");
|
||||||
|
const limit = Number.parseInt(query.limit ?? "50");
|
||||||
|
const skip = (page - 1) * limit;
|
||||||
|
const messages = await database_js_1.prisma.message.findMany({
|
||||||
|
where: {
|
||||||
|
roomId: query.roomId,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
username: true,
|
||||||
|
avatar: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reactions: {
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
username: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
orderBy: {
|
||||||
|
createdAt: "desc",
|
||||||
|
},
|
||||||
|
take: limit,
|
||||||
|
skip,
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
data: messages.reverse(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error("Get messages error:", error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: "Failed to retrieve messages",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static async checkRoomMembership(userId, roomId) {
|
||||||
|
try {
|
||||||
|
const membership = await database_js_1.prisma.chatRoomMember.findUnique({
|
||||||
|
where: {
|
||||||
|
userId_roomId: {
|
||||||
|
userId,
|
||||||
|
roomId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return !!membership;
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error("Check room membership error:", error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.ChatService = ChatService;
|
||||||
108
backend/dist/services/messageService.js
vendored
Normal file
108
backend/dist/services/messageService.js
vendored
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.MessageService = void 0;
|
||||||
|
const database_js_1 = require("../config/database.js");
|
||||||
|
class MessageService {
|
||||||
|
static async sendMessage(userId, data) {
|
||||||
|
try {
|
||||||
|
const message = await database_js_1.prisma.message.create({
|
||||||
|
data: {
|
||||||
|
content: data.constent,
|
||||||
|
userId,
|
||||||
|
roomId: data.roomId,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
username: true,
|
||||||
|
avatar: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reactions: {
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
username: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
data: message,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error("Send message error:", error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: "Failed to send message",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static async reactToMessage(userId, data) {
|
||||||
|
try {
|
||||||
|
const existingReaction = await database_js_1.prisma.messageReaction.findUnique({
|
||||||
|
where: {
|
||||||
|
userId_messageId_type: {
|
||||||
|
userId,
|
||||||
|
messageId: data.messageId,
|
||||||
|
type: data.type,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (existingReaction) {
|
||||||
|
await database_js_1.prisma.messageReaction.delete({
|
||||||
|
where: {
|
||||||
|
id: existingReaction.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
await database_js_1.prisma.messageReaction.create({
|
||||||
|
data: {
|
||||||
|
userId,
|
||||||
|
messageId: data.messageId,
|
||||||
|
type: data.type,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const message = await database_js_1.prisma.message.findUnique({
|
||||||
|
where: {
|
||||||
|
id: data.messageId,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
reactions: {
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
username: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
data: {
|
||||||
|
messageId: data.messageId,
|
||||||
|
reactions: message?.reactions ?? [],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error("React to message error:", error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: "Failed to react to message",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.MessageService = MessageService;
|
||||||
119
backend/dist/socket/socketHandlers.js
vendored
Normal file
119
backend/dist/socket/socketHandlers.js
vendored
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.handleConnection = void 0;
|
||||||
|
const database_js_1 = require("../config/database.js");
|
||||||
|
const rateLimiter_js_1 = require("../middleware/rateLimiter.js");
|
||||||
|
const chatService_js_1 = require("../services/chatService.js");
|
||||||
|
const messageService_js_1 = require("../services/messageService.js");
|
||||||
|
const handleConnection = async (io, socket) => {
|
||||||
|
console.log(`User connected: ${socket.user.username} (${socket.user.id})`);
|
||||||
|
await updateUserOnlineStatus(socket.userId, true);
|
||||||
|
await joinUserRooms(socket);
|
||||||
|
socket.on("send_message", (data) => handleSendMessage(io, socket, data));
|
||||||
|
socket.on("react_to_message", (data) => handleReactToMessage(io, socket, data));
|
||||||
|
socket.on("join_room", (roomId) => handleJoinRoom(socket, roomId));
|
||||||
|
socket.on("leave_room", (roomId) => handleLeaveRoom(socket, roomId));
|
||||||
|
socket.on("disconnect", () => handleDisconnect(socket));
|
||||||
|
};
|
||||||
|
exports.handleConnection = handleConnection;
|
||||||
|
const updateUserOnlineStatus = async (userId, isOnline) => {
|
||||||
|
try {
|
||||||
|
await database_js_1.prisma.user.update({
|
||||||
|
where: { id: userId },
|
||||||
|
data: {
|
||||||
|
isOnline,
|
||||||
|
lastSeen: new Date(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error(`Error updating user online status: ${error}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const joinUserRooms = async (socket) => {
|
||||||
|
try {
|
||||||
|
const userRooms = await database_js_1.prisma.chatRoomMember.findMany({
|
||||||
|
where: { userId: socket.userId },
|
||||||
|
include: { room: true },
|
||||||
|
});
|
||||||
|
userRooms.forEach(async (member) => {
|
||||||
|
await socket.join(member.roomId);
|
||||||
|
socket.to(member.roomId).emit("user_online", {
|
||||||
|
userId: socket.userId,
|
||||||
|
username: socket.user.username,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error(`Error joining user rooms: ${error}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const handleSendMessage = async (io, socket, data) => {
|
||||||
|
try {
|
||||||
|
if (!(0, rateLimiter_js_1.checkMessageRateLimit)(socket))
|
||||||
|
return;
|
||||||
|
const isMember = await chatService_js_1.ChatService.checkRoomMembership(socket.userId, data.roomId);
|
||||||
|
if (!isMember) {
|
||||||
|
socket.emit("error", { message: "Not authorized to send messages to this room" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const result = await messageService_js_1.MessageService.sendMessage(socket.userId, data);
|
||||||
|
if (result.success) {
|
||||||
|
io.to(data.roomId).emit("new_message", result.data);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
socket.emit("error", { message: "Failed to send message" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error(`Error handling send message: ${error}`);
|
||||||
|
socket.emit("error", { message: "Failed to send message" });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const handleReactToMessage = async (io, socket, data) => {
|
||||||
|
try {
|
||||||
|
const result = await messageService_js_1.MessageService.reactToMessage(socket.userId, data);
|
||||||
|
if (result.success) {
|
||||||
|
const message = await database_js_1.prisma.message.findUnique({
|
||||||
|
where: { id: data.messageId },
|
||||||
|
select: { roomId: true },
|
||||||
|
});
|
||||||
|
if (message) {
|
||||||
|
io.to(message.roomId).emit("message_reaction_updated", result.data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
socket.emit("error", { message: result.error });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error(`Error handling react to message: ${error}`);
|
||||||
|
socket.emit("error", { message: "Failed to react to message" });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const handleJoinRoom = async (socket, roomId) => {
|
||||||
|
await socket.join(roomId);
|
||||||
|
};
|
||||||
|
const handleLeaveRoom = async (socket, roomId) => {
|
||||||
|
await socket.leave(roomId);
|
||||||
|
};
|
||||||
|
const handleDisconnect = async (socket) => {
|
||||||
|
console.log(`User disconnected: ${socket.user.username}`);
|
||||||
|
await updateUserOnlineStatus(socket.userId, false);
|
||||||
|
try {
|
||||||
|
const userRooms = await database_js_1.prisma.chatRoomMember.findMany({
|
||||||
|
where: {
|
||||||
|
userId: socket.userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
userRooms.forEach((member) => {
|
||||||
|
socket.to(member.roomId).emit("user_offline", {
|
||||||
|
userId: socket.userId,
|
||||||
|
username: socket.user.username,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error(`Error handling user disconnect: ${error}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
2
backend/dist/types/index.js
vendored
Normal file
2
backend/dist/types/index.js
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
36
backend/eslint.config.js
Normal file
36
backend/eslint.config.js
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
// @ts-check
|
||||||
|
|
||||||
|
import eslint from "@eslint/js";
|
||||||
|
import tseslint from "typescript-eslint";
|
||||||
|
|
||||||
|
export default tseslint.config(
|
||||||
|
{
|
||||||
|
ignores: ["**/*.js"],
|
||||||
|
},
|
||||||
|
eslint.configs.recommended,
|
||||||
|
tseslint.configs.strictTypeChecked,
|
||||||
|
tseslint.configs.stylisticTypeChecked,
|
||||||
|
{
|
||||||
|
languageOptions: {
|
||||||
|
parserOptions: {
|
||||||
|
projectService: true,
|
||||||
|
tsconfigRootDir: import.meta.dirname,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
rules: {
|
||||||
|
"@typescript-eslint/no-misused-promises": "off",
|
||||||
|
"@typescript-eslint/no-unsafe-assignment": "off",
|
||||||
|
"@typescript-eslint/no-unsafe-call": "off",
|
||||||
|
"@typescript-eslint/no-unsafe-member-access": "off",
|
||||||
|
"@typescript-eslint/no-confusing-void-expression": "off",
|
||||||
|
"@typescript-eslint/restrict-template-expressions": "off",
|
||||||
|
"@typescript-eslint/consistent-type-definitions": ["error", "type"],
|
||||||
|
"@typescript-eslint/no-unsafe-argument": "off",
|
||||||
|
"@typescript-eslint/no-extraneous-class": "off",
|
||||||
|
"@typescript-eslint/unbound-method": "off",
|
||||||
|
"@typescript-eslint/no-floating-promises": "off",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
3936
backend/package-lock.json
generated
Normal file
3936
backend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
47
backend/package.json
Normal file
47
backend/package.json
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
{
|
||||||
|
"name": "chat-app-backend",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"description": "Backend of the chat app",
|
||||||
|
"license": "ISC",
|
||||||
|
"author": "",
|
||||||
|
"type": "commonjs",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "tsx --watch --env-file .env src/index.ts",
|
||||||
|
"start": "node --env-file .env dist/index.js",
|
||||||
|
"build": "tsc && tsc-alias",
|
||||||
|
"lint": "eslint .",
|
||||||
|
"lint:fix": "eslint --fix .",
|
||||||
|
"format": "prettier --write .",
|
||||||
|
"db:push": "prisma db push",
|
||||||
|
"db:generate": "prisma generate",
|
||||||
|
"db:seed": "prisma db seed",
|
||||||
|
"db:studio": "prisma studio"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@prisma/client": "^6.9.0",
|
||||||
|
"bcryptjs": "^3.0.2",
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"express": "^5.1.0",
|
||||||
|
"express-rate-limit": "^7.5.0",
|
||||||
|
"jsonwebtoken": "^9.0.2",
|
||||||
|
"socket.io": "^4.8.1",
|
||||||
|
"zod": "^3.25.62"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@eslint/js": "^9.28.0",
|
||||||
|
"@tsconfig/node22": "^22.0.2",
|
||||||
|
"@types/bcryptjs": "^2.4.6",
|
||||||
|
"@types/cors": "^2.8.19",
|
||||||
|
"@types/express": "^5.0.3",
|
||||||
|
"@types/jsonwebtoken": "^9.0.9",
|
||||||
|
"@types/node": "^24.0.0",
|
||||||
|
"eslint": "^9.28.0",
|
||||||
|
"prettier": "^3.5.3",
|
||||||
|
"prisma": "^6.9.0",
|
||||||
|
"tsc-alias": "^1.8.16",
|
||||||
|
"tsconfig-paths": "^4.2.0",
|
||||||
|
"tsx": "^4.20.1",
|
||||||
|
"typescript": "^5.8.3",
|
||||||
|
"typescript-eslint": "^8.34.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
94
backend/prisma/migrations/20250611211149_init/migration.sql
Normal file
94
backend/prisma/migrations/20250611211149_init/migration.sql
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "users" (
|
||||||
|
"id" TEXT NOT NULL,
|
||||||
|
"username" TEXT NOT NULL,
|
||||||
|
"email" TEXT NOT NULL,
|
||||||
|
"password" TEXT NOT NULL,
|
||||||
|
"avatar" TEXT,
|
||||||
|
"isOnline" BOOLEAN NOT NULL DEFAULT false,
|
||||||
|
"lastSeen" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||||
|
|
||||||
|
CONSTRAINT "users_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "chat_rooms" (
|
||||||
|
"id" TEXT NOT NULL,
|
||||||
|
"name" TEXT NOT NULL,
|
||||||
|
"description" TEXT,
|
||||||
|
"isPrivate" BOOLEAN NOT NULL DEFAULT false,
|
||||||
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||||
|
"createdBy" TEXT NOT NULL,
|
||||||
|
|
||||||
|
CONSTRAINT "chat_rooms_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "chat_room_members" (
|
||||||
|
"id" TEXT NOT NULL,
|
||||||
|
"userId" TEXT NOT NULL,
|
||||||
|
"roomId" TEXT NOT NULL,
|
||||||
|
"joinedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"role" TEXT NOT NULL DEFAULT 'member',
|
||||||
|
|
||||||
|
CONSTRAINT "chat_room_members_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "messages" (
|
||||||
|
"id" TEXT NOT NULL,
|
||||||
|
"content" TEXT NOT NULL,
|
||||||
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||||
|
"userId" TEXT NOT NULL,
|
||||||
|
"roomId" TEXT NOT NULL,
|
||||||
|
|
||||||
|
CONSTRAINT "messages_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "message_reactions" (
|
||||||
|
"id" TEXT NOT NULL,
|
||||||
|
"type" TEXT NOT NULL,
|
||||||
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"userId" TEXT NOT NULL,
|
||||||
|
"messageId" TEXT NOT NULL,
|
||||||
|
|
||||||
|
CONSTRAINT "message_reactions_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "users_username_key" ON "users"("username");
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "chat_room_members_userId_roomId_key" ON "chat_room_members"("userId", "roomId");
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "message_reactions_userId_messageId_type_key" ON "message_reactions"("userId", "messageId", "type");
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "chat_rooms" ADD CONSTRAINT "chat_rooms_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "chat_room_members" ADD CONSTRAINT "chat_room_members_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "chat_room_members" ADD CONSTRAINT "chat_room_members_roomId_fkey" FOREIGN KEY ("roomId") REFERENCES "chat_rooms"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "messages" ADD CONSTRAINT "messages_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "messages" ADD CONSTRAINT "messages_roomId_fkey" FOREIGN KEY ("roomId") REFERENCES "chat_rooms"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "message_reactions" ADD CONSTRAINT "message_reactions_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "message_reactions" ADD CONSTRAINT "message_reactions_messageId_fkey" FOREIGN KEY ("messageId") REFERENCES "messages"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
3
backend/prisma/migrations/migration_lock.toml
Normal file
3
backend/prisma/migrations/migration_lock.toml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Please do not edit this file manually
|
||||||
|
# It should be added in your version-control system (e.g., Git)
|
||||||
|
provider = "postgresql"
|
||||||
90
backend/prisma/schema.prisma
Normal file
90
backend/prisma/schema.prisma
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
generator client {
|
||||||
|
provider = "prisma-client-js"
|
||||||
|
}
|
||||||
|
|
||||||
|
datasource db {
|
||||||
|
provider = "postgresql"
|
||||||
|
url = env("DATABASE_URL")
|
||||||
|
}
|
||||||
|
|
||||||
|
model User {
|
||||||
|
id String @id @default(cuid())
|
||||||
|
username String @unique
|
||||||
|
email String @unique
|
||||||
|
password String
|
||||||
|
avatar String?
|
||||||
|
isOnline Boolean @default(false)
|
||||||
|
lastSeen DateTime @default(now())
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
messages Message[]
|
||||||
|
reactions MessageReaction[]
|
||||||
|
chatRooms ChatRoomMember[]
|
||||||
|
createdRooms ChatRoom[]
|
||||||
|
|
||||||
|
@@map("users")
|
||||||
|
}
|
||||||
|
|
||||||
|
model ChatRoom {
|
||||||
|
id String @id @default(cuid())
|
||||||
|
name String
|
||||||
|
description String?
|
||||||
|
isPrivate Boolean @default(false)
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
createdBy String
|
||||||
|
creator User @relation(fields: [createdBy], references: [id])
|
||||||
|
|
||||||
|
messages Message[]
|
||||||
|
members ChatRoomMember[]
|
||||||
|
|
||||||
|
@@map("chat_rooms")
|
||||||
|
}
|
||||||
|
|
||||||
|
model ChatRoomMember {
|
||||||
|
id String @id @default(cuid())
|
||||||
|
userId String
|
||||||
|
roomId String
|
||||||
|
joinedAt DateTime @default(now())
|
||||||
|
role String @default("member") // member, admin
|
||||||
|
|
||||||
|
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||||
|
room ChatRoom @relation(fields: [roomId], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
|
@@unique([userId, roomId])
|
||||||
|
@@map("chat_room_members")
|
||||||
|
}
|
||||||
|
|
||||||
|
model Message {
|
||||||
|
id String @id @default(cuid())
|
||||||
|
content String
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
userId String
|
||||||
|
roomId String
|
||||||
|
|
||||||
|
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||||
|
room ChatRoom @relation(fields: [roomId], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
|
reactions MessageReaction[]
|
||||||
|
|
||||||
|
@@map("messages")
|
||||||
|
}
|
||||||
|
|
||||||
|
model MessageReaction {
|
||||||
|
id String @id @default(cuid())
|
||||||
|
type String // like, love, laugh, etc.
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
|
||||||
|
userId String
|
||||||
|
messageId String
|
||||||
|
|
||||||
|
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||||
|
message Message @relation(fields: [messageId], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
|
@@unique([userId, messageId, type])
|
||||||
|
@@map("message_reactions")
|
||||||
|
}
|
||||||
19
backend/src/config/database.ts
Normal file
19
backend/src/config/database.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { PrismaClient } from "@prisma/client";
|
||||||
|
|
||||||
|
export const prisma = new PrismaClient({
|
||||||
|
log: ["query", "info", "warn", "error"],
|
||||||
|
});
|
||||||
|
|
||||||
|
export const connectDatabase = async (): Promise<void> => {
|
||||||
|
try {
|
||||||
|
await prisma.$connect();
|
||||||
|
console.log("✅ Database connected successfully");
|
||||||
|
} catch (error) {
|
||||||
|
console.error("❌ Database connection failed:", error);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const disconnectDatabase = async (): Promise<void> => {
|
||||||
|
await prisma.$disconnect();
|
||||||
|
};
|
||||||
11
backend/src/config/env.ts
Normal file
11
backend/src/config/env.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
const envSchema = z.object({
|
||||||
|
CORS_ORIGIN: z.string().default("*"),
|
||||||
|
DATABASE_URL: z.string(),
|
||||||
|
JWT_SECRET: z.string(),
|
||||||
|
NODE_ENV: z.enum(["development", "production", "test"]).default("development"),
|
||||||
|
PORT: z.string().transform(Number).default("3000"),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const env = envSchema.parse(process.env);
|
||||||
19
backend/src/controllers/authController.ts
Normal file
19
backend/src/controllers/authController.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { AuthService } from "@/services/authService.js";
|
||||||
|
import { AuthenticatedRequest, LoginRequest, RegisterRequest } from "@/types/index.js";
|
||||||
|
import { Response } from "express";
|
||||||
|
|
||||||
|
export class AuthController {
|
||||||
|
static async register(req: AuthenticatedRequest, res: Response) {
|
||||||
|
const data: RegisterRequest = req.body;
|
||||||
|
const result = await AuthService.register(data);
|
||||||
|
|
||||||
|
res.status(result.success ? 201 : 400).json(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
static async login(req: AuthenticatedRequest, res: Response) {
|
||||||
|
const data: LoginRequest = req.body;
|
||||||
|
const result = await AuthService.login(data);
|
||||||
|
|
||||||
|
res.status(result.success ? 200 : 400).json(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
43
backend/src/controllers/chatController.ts
Normal file
43
backend/src/controllers/chatController.ts
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import { ChatService } from "@/services/chatService.js";
|
||||||
|
import { AuthenticatedRequest, CreateChatRoomRequest, GetMessagesQuery } from "@/types/index.js";
|
||||||
|
import type { Response } from "express";
|
||||||
|
|
||||||
|
export class ChatController {
|
||||||
|
static async getChatRooms(req: AuthenticatedRequest, res: Response) {
|
||||||
|
if (!req.user) {
|
||||||
|
res.status(401).json({ success: false, error: "Unauthorized" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await ChatService.getChatRooms(req.user.userId);
|
||||||
|
res.status(result.success ? 200 : 500).json(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
static async createChatRoom(req: AuthenticatedRequest, res: Response) {
|
||||||
|
if (!req.user) {
|
||||||
|
res.status(401).json({ success: false, error: "Unauthorized" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const data: CreateChatRoomRequest = req.body;
|
||||||
|
const result = await ChatService.createChatRoom(req.user.userId, data);
|
||||||
|
|
||||||
|
res.status(result.success ? 201 : 500).json(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
static async getMessages(req: AuthenticatedRequest, res: Response) {
|
||||||
|
if (!req.user) {
|
||||||
|
res.status(401).json({ success: false, error: "Unauthorized" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const query: GetMessagesQuery = {
|
||||||
|
roomId: req.params.roomId,
|
||||||
|
page: req.query.page as string,
|
||||||
|
limit: req.query.limit as string,
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await ChatService.getMessages(query);
|
||||||
|
res.status(result.success ? 200 : 500).json(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
71
backend/src/index.ts
Normal file
71
backend/src/index.ts
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
import cors from "cors";
|
||||||
|
import express, { Request, Response } from "express";
|
||||||
|
import http from "http";
|
||||||
|
import { Server } from "socket.io";
|
||||||
|
import { connectDatabase } from "./config/database.js";
|
||||||
|
import { env } from "./config/env.js";
|
||||||
|
import { authenticateSocket } from "./middleware/auth.js";
|
||||||
|
import { handleConnection } from "./socket/socketHandlers.js";
|
||||||
|
import { AuthenticatedSocket } from "./types/index.js";
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
const server = http.createServer(app);
|
||||||
|
const io = new Server(server, {
|
||||||
|
cors: {
|
||||||
|
methods: ["GET", "POST"],
|
||||||
|
origin: "http://localhost:5173",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
app.use(
|
||||||
|
cors({
|
||||||
|
methods: ["GET", "POST"],
|
||||||
|
origin: "http://localhost:5173",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
app.use(express.json());
|
||||||
|
|
||||||
|
app.get("/health", (_, res) => {
|
||||||
|
res.json({ status: "OK", timestamp: new Date().toISOString() });
|
||||||
|
});
|
||||||
|
|
||||||
|
io.use(authenticateSocket);
|
||||||
|
io.on("connection", async (socket) => {
|
||||||
|
await handleConnection(io, socket as AuthenticatedSocket);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.use((err: Error, _: Request, res: Response) => {
|
||||||
|
console.error("Unhandled error:", err);
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
error: env.NODE_ENV === "production" ? "Internal server error" : err.message,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const startServer = async () => {
|
||||||
|
try {
|
||||||
|
await connectDatabase();
|
||||||
|
|
||||||
|
server.listen(env.PORT, () => {
|
||||||
|
console.log(`🚀 Server running on port ${env.PORT}`);
|
||||||
|
console.log(`📊 Environment: ${env.NODE_ENV}`);
|
||||||
|
console.log(`🔗 CORS Origin: ${env.CORS_ORIGIN}`);
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to start server:", error);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
process.on("SIGTERM", () => {
|
||||||
|
console.log("SIGTERM received, shutting down gracefully...");
|
||||||
|
server.close(() => {
|
||||||
|
console.log("HTTP server closed");
|
||||||
|
process.exit(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
await startServer();
|
||||||
|
})()
|
||||||
54
backend/src/middleware/auth.ts
Normal file
54
backend/src/middleware/auth.ts
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import { NextFunction, Response } from "express";
|
||||||
|
import jwt from "jsonwebtoken";
|
||||||
|
|
||||||
|
import { prisma } from "@/config/database.js";
|
||||||
|
import { env } from "@/config/env.js";
|
||||||
|
import { AuthenticatedRequest, AuthenticatedSocket, JWTPayload } from "@/types/index.js";
|
||||||
|
import { Socket } from "socket.io";
|
||||||
|
|
||||||
|
export const authenticateToken = (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
|
||||||
|
try {
|
||||||
|
const authHeader = req.headers.authorization;
|
||||||
|
const token = authHeader?.split(" ")[1];
|
||||||
|
|
||||||
|
if (!token) {
|
||||||
|
res.status(401).json({ success: false, error: "Access token required" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const decoded = jwt.verify(token, env.JWT_SECRET) as JWTPayload;
|
||||||
|
req.user = decoded;
|
||||||
|
next();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Authentication error:", error);
|
||||||
|
res.status(403).json({ success: false, error: "Invalid or expired token" });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const authenticateSocket = async (socket: Socket, next: (err?: Error) => void) => {
|
||||||
|
try {
|
||||||
|
const token = socket.handshake.auth.token as string | undefined;
|
||||||
|
|
||||||
|
if (!token) {
|
||||||
|
return next(new Error("Authentication token required"));
|
||||||
|
}
|
||||||
|
|
||||||
|
const decoded = jwt.verify(token, env.JWT_SECRET) as JWTPayload;
|
||||||
|
|
||||||
|
const user = await prisma.user.findUnique({
|
||||||
|
where: {
|
||||||
|
id: decoded.userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!user) return next(new Error("User not found"));
|
||||||
|
|
||||||
|
const authenticateSocket = socket as AuthenticatedSocket;
|
||||||
|
authenticateSocket.userId = user.id;
|
||||||
|
authenticateSocket.user = user;
|
||||||
|
|
||||||
|
next();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Socket authentication error:", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
56
backend/src/middleware/rateLimiter.ts
Normal file
56
backend/src/middleware/rateLimiter.ts
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
import { AuthenticatedSocket } from "@/types/index.js";
|
||||||
|
|
||||||
|
type RateLimitData = {
|
||||||
|
count: number;
|
||||||
|
resetTime: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
class RateLimiter {
|
||||||
|
private limits = new Map<string, RateLimitData>();
|
||||||
|
private readonly maxMessages: number;
|
||||||
|
private readonly windowMs: number;
|
||||||
|
|
||||||
|
constructor(maxMessages = 10, windowMs = 60000) {
|
||||||
|
this.maxMessages = maxMessages;
|
||||||
|
this.windowMs = windowMs;
|
||||||
|
}
|
||||||
|
|
||||||
|
checkLimit(userId: string) {
|
||||||
|
const now = Date.now();
|
||||||
|
const userLimit = this.limits.get(userId) ?? { count: 0, resetTime: now + this.windowMs };
|
||||||
|
|
||||||
|
if (now > userLimit.resetTime) {
|
||||||
|
userLimit.count = 0;
|
||||||
|
userLimit.resetTime = now + this.windowMs;
|
||||||
|
}
|
||||||
|
|
||||||
|
userLimit.count++;
|
||||||
|
this.limits.set(userId, userLimit);
|
||||||
|
|
||||||
|
return userLimit.count <= this.maxMessages;
|
||||||
|
}
|
||||||
|
|
||||||
|
getRemainingTime(userId: string) {
|
||||||
|
const userLimit = this.limits.get(userId);
|
||||||
|
if (!userLimit) return 0;
|
||||||
|
|
||||||
|
return Math.max(0, userLimit.resetTime - Date.now());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const messageRateLimiter = new RateLimiter(10, 60000); // 10 messages per minute
|
||||||
|
|
||||||
|
export const checkMessageRateLimit = (socket: AuthenticatedSocket) => {
|
||||||
|
const canSend = messageRateLimiter.checkLimit(socket.userId);
|
||||||
|
|
||||||
|
if (!canSend) {
|
||||||
|
const remainingTime = messageRateLimiter.getRemainingTime(socket.userId);
|
||||||
|
|
||||||
|
socket.emit("rate_limit_exceeded", {
|
||||||
|
message: `Too many messages. Please wait ${Math.ceil(remainingTime / 1000)} seconds before sending another message.`,
|
||||||
|
remainingTime,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return canSend;
|
||||||
|
};
|
||||||
38
backend/src/middleware/validation.ts
Normal file
38
backend/src/middleware/validation.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import type { NextFunction, Request, Response } from "express";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
export const validate = (schema: z.ZodSchema) => {
|
||||||
|
return (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
try {
|
||||||
|
schema.parse(req.body);
|
||||||
|
next();
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof z.ZodError) {
|
||||||
|
res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
error: "Validation failed",
|
||||||
|
details: error.errors,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export const loginSchema = z.object({
|
||||||
|
email: z.string().email("Invalid email format"),
|
||||||
|
password: z.string().min(6, "Password must be at least 6 characters long"),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const registerSchema = z.object({
|
||||||
|
username: z.string().min(3, "Username must be at least 3 characters long"),
|
||||||
|
email: z.string().email("Invalid email format"),
|
||||||
|
password: z.string().min(6, "Password must be at least 6 characters long"),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const createChatRoomSchema = z.object({
|
||||||
|
name: z.string().min(1, "Room name is required"),
|
||||||
|
description: z.string().optional(),
|
||||||
|
memberUsernames: z.array(z.string()).default([]),
|
||||||
|
});
|
||||||
10
backend/src/routes/authRoutes.ts
Normal file
10
backend/src/routes/authRoutes.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { AuthController } from "@/controllers/authController.js";
|
||||||
|
import { loginSchema, registerSchema, validate } from "@/middleware/validation.js";
|
||||||
|
import { Router } from "express";
|
||||||
|
|
||||||
|
const router = Router();
|
||||||
|
|
||||||
|
router.post("/register", validate(registerSchema), AuthController.register);
|
||||||
|
router.post("/login", validate(loginSchema), AuthController.login);
|
||||||
|
|
||||||
|
export { router as authRoutes };
|
||||||
14
backend/src/routes/chatRoutes.ts
Normal file
14
backend/src/routes/chatRoutes.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import { ChatController } from "@/controllers/chatController.js";
|
||||||
|
import { authenticateToken } from "@/middleware/auth.js";
|
||||||
|
import { createChatRoomSchema, validate } from "@/middleware/validation.js";
|
||||||
|
import { Router } from "express";
|
||||||
|
|
||||||
|
const router = Router();
|
||||||
|
|
||||||
|
router.use(authenticateToken);
|
||||||
|
|
||||||
|
router.get("/chat-rooms", ChatController.getChatRooms);
|
||||||
|
router.post("/chat-rooms", validate(createChatRoomSchema), ChatController.createChatRoom);
|
||||||
|
router.get("/messages/:roomId", ChatController.getMessages);
|
||||||
|
|
||||||
|
export { router as chatRoutes };
|
||||||
10
backend/src/routes/index.ts
Normal file
10
backend/src/routes/index.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { Router } from "express";
|
||||||
|
import { authRoutes } from "./authRoutes.js";
|
||||||
|
import { chatRoutes } from "./chatRoutes.js";
|
||||||
|
|
||||||
|
const router = Router();
|
||||||
|
|
||||||
|
router.use("/auth", authRoutes);
|
||||||
|
router.use("/", chatRoutes);
|
||||||
|
|
||||||
|
export { router as apiRoutes };
|
||||||
102
backend/src/services/authService.ts
Normal file
102
backend/src/services/authService.ts
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
import { prisma } from "@/config/database.js";
|
||||||
|
import { env } from "@/config/env.js";
|
||||||
|
import { ApiResponse, JWTPayload, LoginRequest, RegisterRequest } from "@/types/index.js";
|
||||||
|
import bcrypt from "bcryptjs";
|
||||||
|
import jwt from "jsonwebtoken";
|
||||||
|
|
||||||
|
export class AuthService {
|
||||||
|
static async register(data: RegisterRequest): Promise<ApiResponse> {
|
||||||
|
try {
|
||||||
|
const existingUser = await prisma.user.findFirst({
|
||||||
|
where: {
|
||||||
|
OR: [{ email: data.email }, { username: data.username }],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (existingUser) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: existingUser.email === data.email ? "Email already exists" : "Username already exists",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
const hashedPassword = await bcrypt.hash(data.password, 12);
|
||||||
|
|
||||||
|
const user = await prisma.user.create({
|
||||||
|
data: {
|
||||||
|
username: data.username,
|
||||||
|
email: data.email,
|
||||||
|
password: hashedPassword,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const token = this.generateToken({
|
||||||
|
userId: user.id,
|
||||||
|
username: user.username,
|
||||||
|
email: user.email,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
data: {
|
||||||
|
token,
|
||||||
|
user: {
|
||||||
|
id: user.id,
|
||||||
|
username: user.username,
|
||||||
|
email: user.email,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Registration error:", error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: "Registration failed",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async login(data: LoginRequest): Promise<ApiResponse> {
|
||||||
|
try {
|
||||||
|
const user = await prisma.user.findUnique({
|
||||||
|
where: {
|
||||||
|
email: data.email,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!user || !(await bcrypt.compare(data.password, user.password))) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: "Invalid email or password",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const token = this.generateToken({
|
||||||
|
userId: user.id,
|
||||||
|
username: user.username,
|
||||||
|
email: user.email,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
data: {
|
||||||
|
token,
|
||||||
|
user: {
|
||||||
|
id: user.id,
|
||||||
|
username: user.username,
|
||||||
|
email: user.email,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Login error:", error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: "Login failed",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static generateToken(payload: JWTPayload): string {
|
||||||
|
return jwt.sign(payload, env.JWT_SECRET, { expiresIn: "7d" });
|
||||||
|
}
|
||||||
|
}
|
||||||
193
backend/src/services/chatService.ts
Normal file
193
backend/src/services/chatService.ts
Normal file
@ -0,0 +1,193 @@
|
|||||||
|
import { prisma } from "@/config/database.js";
|
||||||
|
import { ApiResponse, CreateChatRoomRequest, GetMessagesQuery } from "@/types/index.js";
|
||||||
|
|
||||||
|
export class ChatService {
|
||||||
|
static async getChatRooms(userId: string): Promise<ApiResponse> {
|
||||||
|
try {
|
||||||
|
const chatRooms = await prisma.chatRoom.findMany({
|
||||||
|
where: {
|
||||||
|
members: {
|
||||||
|
some: {
|
||||||
|
userId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
members: {
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
username: true,
|
||||||
|
isOnline: true,
|
||||||
|
avatar: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
_count: {
|
||||||
|
select: {
|
||||||
|
messages: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
orderBy: {
|
||||||
|
updatedAt: "desc",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
data: chatRooms,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Get chat rooms error:", error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: "Failed to retrieve chat rooms",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async createChatRoom(userId: string, data: CreateChatRoomRequest): Promise<ApiResponse> {
|
||||||
|
try {
|
||||||
|
const chatRoom = await prisma.chatRoom.create({
|
||||||
|
data: {
|
||||||
|
name: data.name,
|
||||||
|
description: data.description,
|
||||||
|
createdBy: userId,
|
||||||
|
members: {
|
||||||
|
create: [
|
||||||
|
{
|
||||||
|
userId,
|
||||||
|
role: "admin",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (data.memberUsernames.length > 0) {
|
||||||
|
const users = await prisma.user.findMany({
|
||||||
|
where: {
|
||||||
|
username: {
|
||||||
|
in: data.memberUsernames,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const memberData = users.map((user) => ({
|
||||||
|
userId: user.id,
|
||||||
|
roomId: chatRoom.id,
|
||||||
|
}));
|
||||||
|
|
||||||
|
await prisma.chatRoomMember.createMany({
|
||||||
|
data: memberData,
|
||||||
|
skipDuplicates: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const fullChatRoom = await prisma.chatRoom.findUnique({
|
||||||
|
where: { id: chatRoom.id },
|
||||||
|
include: {
|
||||||
|
members: {
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
username: true,
|
||||||
|
isOnline: true,
|
||||||
|
avatar: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
_count: {
|
||||||
|
select: {
|
||||||
|
messages: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
data: fullChatRoom,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Create chat room error:", error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: "Failed to create chat room",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async getMessages(query: GetMessagesQuery): Promise<ApiResponse> {
|
||||||
|
try {
|
||||||
|
const page = Number.parseInt(query.page ?? "1");
|
||||||
|
const limit = Number.parseInt(query.limit ?? "50");
|
||||||
|
const skip = (page - 1) * limit;
|
||||||
|
|
||||||
|
const messages = await prisma.message.findMany({
|
||||||
|
where: {
|
||||||
|
roomId: query.roomId,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
username: true,
|
||||||
|
avatar: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reactions: {
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
username: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
orderBy: {
|
||||||
|
createdAt: "desc",
|
||||||
|
},
|
||||||
|
take: limit,
|
||||||
|
skip,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
data: messages.reverse(),
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Get messages error:", error);
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: "Failed to retrieve messages",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async checkRoomMembership(userId: string, roomId: string) {
|
||||||
|
try {
|
||||||
|
const membership = await prisma.chatRoomMember.findUnique({
|
||||||
|
where: {
|
||||||
|
userId_roomId: {
|
||||||
|
userId,
|
||||||
|
roomId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return !!membership;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Check room membership error:", error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
109
backend/src/services/messageService.ts
Normal file
109
backend/src/services/messageService.ts
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
import { prisma } from "@/config/database.js";
|
||||||
|
import { ApiResponse, ReactToMessageRequest, SendMessageRequest } from "@/types/index.js";
|
||||||
|
|
||||||
|
export class MessageService {
|
||||||
|
static async sendMessage(userId: string, data: SendMessageRequest): Promise<ApiResponse> {
|
||||||
|
try {
|
||||||
|
const message = await prisma.message.create({
|
||||||
|
data: {
|
||||||
|
content: data.constent,
|
||||||
|
userId,
|
||||||
|
roomId: data.roomId,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
username: true,
|
||||||
|
avatar: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reactions: {
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
username: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
data: message,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Send message error:", error);
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: "Failed to send message",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async reactToMessage(userId: string, data: ReactToMessageRequest): Promise<ApiResponse> {
|
||||||
|
try {
|
||||||
|
const existingReaction = await prisma.messageReaction.findUnique({
|
||||||
|
where: {
|
||||||
|
userId_messageId_type: {
|
||||||
|
userId,
|
||||||
|
messageId: data.messageId,
|
||||||
|
type: data.type,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (existingReaction) {
|
||||||
|
await prisma.messageReaction.delete({
|
||||||
|
where: {
|
||||||
|
id: existingReaction.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
await prisma.messageReaction.create({
|
||||||
|
data: {
|
||||||
|
userId,
|
||||||
|
messageId: data.messageId,
|
||||||
|
type: data.type,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const message = await prisma.message.findUnique({
|
||||||
|
where: {
|
||||||
|
id: data.messageId,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
reactions: {
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
username: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
data: {
|
||||||
|
messageId: data.messageId,
|
||||||
|
reactions: message?.reactions ?? [],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error("React to message error:", error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: "Failed to react to message",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
133
backend/src/socket/socketHandlers.ts
Normal file
133
backend/src/socket/socketHandlers.ts
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
import { prisma } from "@/config/database.js";
|
||||||
|
import { checkMessageRateLimit } from "@/middleware/rateLimiter.js";
|
||||||
|
import { ChatService } from "@/services/chatService.js";
|
||||||
|
import { MessageService } from "@/services/messageService.js";
|
||||||
|
import { AuthenticatedSocket, ReactToMessageRequest, SendMessageRequest } from "@/types/index.js";
|
||||||
|
import { Server } from "socket.io";
|
||||||
|
|
||||||
|
export const handleConnection = async (io: Server, socket: AuthenticatedSocket) => {
|
||||||
|
console.log(`User connected: ${socket.user.username} (${socket.user.id})`);
|
||||||
|
|
||||||
|
await updateUserOnlineStatus(socket.userId, true);
|
||||||
|
|
||||||
|
await joinUserRooms(socket);
|
||||||
|
|
||||||
|
socket.on("send_message", (data: SendMessageRequest) => handleSendMessage(io, socket, data));
|
||||||
|
|
||||||
|
socket.on("react_to_message", (data: ReactToMessageRequest) => handleReactToMessage(io, socket, data));
|
||||||
|
|
||||||
|
socket.on("join_room", (roomId: string) => handleJoinRoom(socket, roomId));
|
||||||
|
socket.on("leave_room", (roomId: string) => handleLeaveRoom(socket, roomId));
|
||||||
|
|
||||||
|
socket.on("disconnect", () => handleDisconnect(socket));
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateUserOnlineStatus = async (userId: string, isOnline: boolean) => {
|
||||||
|
try {
|
||||||
|
await prisma.user.update({
|
||||||
|
where: { id: userId },
|
||||||
|
data: {
|
||||||
|
isOnline,
|
||||||
|
lastSeen: new Date(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error updating user online status: ${error}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const joinUserRooms = async (socket: AuthenticatedSocket) => {
|
||||||
|
try {
|
||||||
|
const userRooms = await prisma.chatRoomMember.findMany({
|
||||||
|
where: { userId: socket.userId },
|
||||||
|
include: { room: true },
|
||||||
|
});
|
||||||
|
|
||||||
|
userRooms.forEach(async (member) => {
|
||||||
|
await socket.join(member.roomId);
|
||||||
|
socket.to(member.roomId).emit("user_online", {
|
||||||
|
userId: socket.userId,
|
||||||
|
username: socket.user.username,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error joining user rooms: ${error}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSendMessage = async (io: Server, socket: AuthenticatedSocket, data: SendMessageRequest) => {
|
||||||
|
try {
|
||||||
|
if (!checkMessageRateLimit(socket)) return;
|
||||||
|
|
||||||
|
const isMember = await ChatService.checkRoomMembership(socket.userId, data.roomId);
|
||||||
|
|
||||||
|
if (!isMember) {
|
||||||
|
socket.emit("error", { message: "Not authorized to send messages to this room" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await MessageService.sendMessage(socket.userId, data);
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
io.to(data.roomId).emit("new_message", result.data);
|
||||||
|
} else {
|
||||||
|
socket.emit("error", { message: "Failed to send message" });
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error handling send message: ${error}`);
|
||||||
|
socket.emit("error", { message: "Failed to send message" });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleReactToMessage = async (io: Server, socket: AuthenticatedSocket, data: ReactToMessageRequest) => {
|
||||||
|
try {
|
||||||
|
const result = await MessageService.reactToMessage(socket.userId, data);
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
const message = await prisma.message.findUnique({
|
||||||
|
where: { id: data.messageId },
|
||||||
|
select: { roomId: true },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (message) {
|
||||||
|
io.to(message.roomId).emit("message_reaction_updated", result.data);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
socket.emit("error", { message: result.error });
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error handling react to message: ${error}`);
|
||||||
|
socket.emit("error", { message: "Failed to react to message" });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleJoinRoom = async (socket: AuthenticatedSocket, roomId: string) => {
|
||||||
|
await socket.join(roomId);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleLeaveRoom = async (socket: AuthenticatedSocket, roomId: string) => {
|
||||||
|
await socket.leave(roomId);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDisconnect = async (socket: AuthenticatedSocket) => {
|
||||||
|
console.log(`User disconnected: ${socket.user.username}`);
|
||||||
|
|
||||||
|
await updateUserOnlineStatus(socket.userId, false);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const userRooms = await prisma.chatRoomMember.findMany({
|
||||||
|
where: {
|
||||||
|
userId: socket.userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
userRooms.forEach((member) => {
|
||||||
|
socket.to(member.roomId).emit("user_offline", {
|
||||||
|
userId: socket.userId,
|
||||||
|
username: socket.user.username,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error handling user disconnect: ${error}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
66
backend/src/types/index.ts
Normal file
66
backend/src/types/index.ts
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import { User } from "@prisma/client";
|
||||||
|
import { Request } from "express";
|
||||||
|
import { Socket } from "socket.io";
|
||||||
|
|
||||||
|
export type AuthenticatedRequest = Request & {
|
||||||
|
user?: {
|
||||||
|
userId: string;
|
||||||
|
email: string;
|
||||||
|
username: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export type JWTPayload = {
|
||||||
|
userId: string;
|
||||||
|
email: string;
|
||||||
|
username: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type AuthenticatedSocket = Socket & {
|
||||||
|
userId: string;
|
||||||
|
user: User;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type SendMessageRequest = {
|
||||||
|
roomId: string;
|
||||||
|
constent: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ReactToMessageRequest = {
|
||||||
|
messageId: string;
|
||||||
|
type: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
export type ApiResponse<T = any> = {
|
||||||
|
success: boolean;
|
||||||
|
data?: T;
|
||||||
|
error?: string;
|
||||||
|
message?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type CreateChatRoomRequest = {
|
||||||
|
name: string;
|
||||||
|
description?: string;
|
||||||
|
memberUsernames: string[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export type PaginationQuery = {
|
||||||
|
page?: string;
|
||||||
|
limit?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type GetMessagesQuery = PaginationQuery & {
|
||||||
|
roomId: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type RegisterRequest = {
|
||||||
|
username: string;
|
||||||
|
email: string;
|
||||||
|
password: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type LoginRequest = {
|
||||||
|
email: string;
|
||||||
|
password: string;
|
||||||
|
};
|
||||||
11
backend/tsconfig.json
Normal file
11
backend/tsconfig.json
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"extends": "@tsconfig/node22/tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"outDir": "./dist",
|
||||||
|
"baseUrl": ".",
|
||||||
|
"paths": {
|
||||||
|
"@/*": ["src/*"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"include": ["src/**/*"]
|
||||||
|
}
|
||||||
24
frontend/.gitignore
vendored
Normal file
24
frontend/.gitignore
vendored
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
pnpm-debug.log*
|
||||||
|
lerna-debug.log*
|
||||||
|
|
||||||
|
node_modules
|
||||||
|
dist
|
||||||
|
dist-ssr
|
||||||
|
*.local
|
||||||
|
|
||||||
|
# Editor directories and files
|
||||||
|
.vscode/*
|
||||||
|
!.vscode/extensions.json
|
||||||
|
.idea
|
||||||
|
.DS_Store
|
||||||
|
*.suo
|
||||||
|
*.ntvs*
|
||||||
|
*.njsproj
|
||||||
|
*.sln
|
||||||
|
*.sw?
|
||||||
10
frontend/.prettierrc
Normal file
10
frontend/.prettierrc
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"semi": true,
|
||||||
|
"singleQuote": true,
|
||||||
|
"trailingComma": "es5",
|
||||||
|
"printWidth": 80,
|
||||||
|
"tabWidth": 2,
|
||||||
|
"jsxSingleQuote": false,
|
||||||
|
"bracketSpacing": true,
|
||||||
|
"arrowParens": "always"
|
||||||
|
}
|
||||||
54
frontend/README.md
Normal file
54
frontend/README.md
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
# React + TypeScript + Vite
|
||||||
|
|
||||||
|
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
|
||||||
|
|
||||||
|
Currently, two official plugins are available:
|
||||||
|
|
||||||
|
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Babel](https://babeljs.io/) for Fast Refresh
|
||||||
|
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
|
||||||
|
|
||||||
|
## Expanding the ESLint configuration
|
||||||
|
|
||||||
|
If you are developing a production application, we recommend updating the configuration to enable type-aware lint rules:
|
||||||
|
|
||||||
|
```js
|
||||||
|
export default tseslint.config({
|
||||||
|
extends: [
|
||||||
|
// Remove ...tseslint.configs.recommended and replace with this
|
||||||
|
...tseslint.configs.recommendedTypeChecked,
|
||||||
|
// Alternatively, use this for stricter rules
|
||||||
|
...tseslint.configs.strictTypeChecked,
|
||||||
|
// Optionally, add this for stylistic rules
|
||||||
|
...tseslint.configs.stylisticTypeChecked,
|
||||||
|
],
|
||||||
|
languageOptions: {
|
||||||
|
// other options...
|
||||||
|
parserOptions: {
|
||||||
|
project: ['./tsconfig.node.json', './tsconfig.app.json'],
|
||||||
|
tsconfigRootDir: import.meta.dirname,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also install [eslint-plugin-react-x](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x) and [eslint-plugin-react-dom](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-dom) for React-specific lint rules:
|
||||||
|
|
||||||
|
```js
|
||||||
|
// eslint.config.js
|
||||||
|
import reactX from 'eslint-plugin-react-x'
|
||||||
|
import reactDom from 'eslint-plugin-react-dom'
|
||||||
|
|
||||||
|
export default tseslint.config({
|
||||||
|
plugins: {
|
||||||
|
// Add the react-x and react-dom plugins
|
||||||
|
'react-x': reactX,
|
||||||
|
'react-dom': reactDom,
|
||||||
|
},
|
||||||
|
rules: {
|
||||||
|
// other rules...
|
||||||
|
// Enable its recommended typescript rules
|
||||||
|
...reactX.configs['recommended-typescript'].rules,
|
||||||
|
...reactDom.configs.recommended.rules,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
```
|
||||||
25
frontend/eslint.config.js
Normal file
25
frontend/eslint.config.js
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import js from "@eslint/js";
|
||||||
|
import globals from "globals";
|
||||||
|
import tseslint from "typescript-eslint";
|
||||||
|
import pluginReact from "eslint-plugin-react";
|
||||||
|
import { defineConfig } from "eslint/config";
|
||||||
|
|
||||||
|
|
||||||
|
export default defineConfig([
|
||||||
|
{ files: ["**/*.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"], plugins: { js }, extends: ["js/recommended"] },
|
||||||
|
{ files: ["**/*.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"], languageOptions: { globals: globals.browser } },
|
||||||
|
tseslint.configs.recommended,
|
||||||
|
pluginReact.configs.flat.recommended,
|
||||||
|
{
|
||||||
|
files: ["**/*.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"],
|
||||||
|
rules: {
|
||||||
|
"react/jsx-uses-react": "off",
|
||||||
|
"react/react-in-jsx-scope": "off",
|
||||||
|
"react/jsx-filename-extension": ["error", { extensions: [".jsx", ".tsx"] }],
|
||||||
|
"react/prop-types": "off",
|
||||||
|
"react/no-unescaped-entities": "off",
|
||||||
|
"react/jsx-no-target-blank": "warn",
|
||||||
|
"react/jsx-key": "warn"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]);
|
||||||
13
frontend/index.html
Normal file
13
frontend/index.html
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>Vite + React + TS</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="root"></div>
|
||||||
|
<script type="module" src="/src/main.tsx"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
4954
frontend/package-lock.json
generated
Normal file
4954
frontend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
32
frontend/package.json
Normal file
32
frontend/package.json
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
{
|
||||||
|
"name": "frontend",
|
||||||
|
"private": true,
|
||||||
|
"version": "0.0.0",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "vite",
|
||||||
|
"build": "tsc -b && vite build",
|
||||||
|
"lint": "eslint .",
|
||||||
|
"lint:fix": "eslint --fix .",
|
||||||
|
"format": "prettier --write .",
|
||||||
|
"preview": "vite preview"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"react": "^19.1.0",
|
||||||
|
"react-dom": "^19.1.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@eslint/js": "^9.28.0",
|
||||||
|
"@types/react": "^19.1.2",
|
||||||
|
"@types/react-dom": "^19.1.2",
|
||||||
|
"@vitejs/plugin-react-swc": "^3.9.0",
|
||||||
|
"eslint": "^9.28.0",
|
||||||
|
"eslint-config-prettier": "^10.1.5",
|
||||||
|
"eslint-plugin-react": "^7.37.5",
|
||||||
|
"globals": "^16.2.0",
|
||||||
|
"prettier": "^3.5.3",
|
||||||
|
"typescript": "~5.8.3",
|
||||||
|
"typescript-eslint": "^8.34.0",
|
||||||
|
"vite": "^6.3.5"
|
||||||
|
}
|
||||||
|
}
|
||||||
1
frontend/public/vite.svg
Normal file
1
frontend/public/vite.svg
Normal file
@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|
||||||
|
After Width: | Height: | Size: 1.5 KiB |
42
frontend/src/App.css
Normal file
42
frontend/src/App.css
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#root {
|
||||||
|
max-width: 1280px;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 2rem;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.logo {
|
||||||
|
height: 6em;
|
||||||
|
padding: 1.5em;
|
||||||
|
will-change: filter;
|
||||||
|
transition: filter 300ms;
|
||||||
|
}
|
||||||
|
.logo:hover {
|
||||||
|
filter: drop-shadow(0 0 2em #646cffaa);
|
||||||
|
}
|
||||||
|
.logo.react:hover {
|
||||||
|
filter: drop-shadow(0 0 2em #61dafbaa);
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes logo-spin {
|
||||||
|
from {
|
||||||
|
transform: rotate(0deg);
|
||||||
|
}
|
||||||
|
to {
|
||||||
|
transform: rotate(360deg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-reduced-motion: no-preference) {
|
||||||
|
a:nth-of-type(2) .logo {
|
||||||
|
animation: logo-spin infinite 20s linear;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.card {
|
||||||
|
padding: 2em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.read-the-docs {
|
||||||
|
color: #888;
|
||||||
|
}
|
||||||
35
frontend/src/App.tsx
Normal file
35
frontend/src/App.tsx
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { useState } from 'react'
|
||||||
|
import reactLogo from './assets/react.svg'
|
||||||
|
import viteLogo from '/vite.svg'
|
||||||
|
import './App.css'
|
||||||
|
|
||||||
|
function App() {
|
||||||
|
const [count, setCount] = useState(0)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div>
|
||||||
|
<a href="https://vite.dev" target="_blank" rel="noreferrer">
|
||||||
|
<img src={viteLogo} className="logo" alt="Vite logo" />
|
||||||
|
</a>
|
||||||
|
<a href="https://react.dev" target="_blank" rel="noreferrer">
|
||||||
|
<img src={reactLogo} className="logo react" alt="React logo" />
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
<h1>Vite + React</h1>
|
||||||
|
<div className="card">
|
||||||
|
<button onClick={() => setCount((count) => count + 1)}>
|
||||||
|
count is {count}
|
||||||
|
</button>
|
||||||
|
<p>
|
||||||
|
Edit <code>src/App.tsx</code> and save to test HMR
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<p className="read-the-docs">
|
||||||
|
Click on the Vite and React logos to learn more
|
||||||
|
</p>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default App
|
||||||
1
frontend/src/assets/react.svg
Normal file
1
frontend/src/assets/react.svg
Normal file
@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="35.93" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 228"><path fill="#00D8FF" d="M210.483 73.824a171.49 171.49 0 0 0-8.24-2.597c.465-1.9.893-3.777 1.273-5.621c6.238-30.281 2.16-54.676-11.769-62.708c-13.355-7.7-35.196.329-57.254 19.526a171.23 171.23 0 0 0-6.375 5.848a155.866 155.866 0 0 0-4.241-3.917C100.759 3.829 77.587-4.822 63.673 3.233C50.33 10.957 46.379 33.89 51.995 62.588a170.974 170.974 0 0 0 1.892 8.48c-3.28.932-6.445 1.924-9.474 2.98C17.309 83.498 0 98.307 0 113.668c0 15.865 18.582 31.778 46.812 41.427a145.52 145.52 0 0 0 6.921 2.165a167.467 167.467 0 0 0-2.01 9.138c-5.354 28.2-1.173 50.591 12.134 58.266c13.744 7.926 36.812-.22 59.273-19.855a145.567 145.567 0 0 0 5.342-4.923a168.064 168.064 0 0 0 6.92 6.314c21.758 18.722 43.246 26.282 56.54 18.586c13.731-7.949 18.194-32.003 12.4-61.268a145.016 145.016 0 0 0-1.535-6.842c1.62-.48 3.21-.974 4.76-1.488c29.348-9.723 48.443-25.443 48.443-41.52c0-15.417-17.868-30.326-45.517-39.844Zm-6.365 70.984c-1.4.463-2.836.91-4.3 1.345c-3.24-10.257-7.612-21.163-12.963-32.432c5.106-11 9.31-21.767 12.459-31.957c2.619.758 5.16 1.557 7.61 2.4c23.69 8.156 38.14 20.213 38.14 29.504c0 9.896-15.606 22.743-40.946 31.14Zm-10.514 20.834c2.562 12.94 2.927 24.64 1.23 33.787c-1.524 8.219-4.59 13.698-8.382 15.893c-8.067 4.67-25.32-1.4-43.927-17.412a156.726 156.726 0 0 1-6.437-5.87c7.214-7.889 14.423-17.06 21.459-27.246c12.376-1.098 24.068-2.894 34.671-5.345a134.17 134.17 0 0 1 1.386 6.193ZM87.276 214.515c-7.882 2.783-14.16 2.863-17.955.675c-8.075-4.657-11.432-22.636-6.853-46.752a156.923 156.923 0 0 1 1.869-8.499c10.486 2.32 22.093 3.988 34.498 4.994c7.084 9.967 14.501 19.128 21.976 27.15a134.668 134.668 0 0 1-4.877 4.492c-9.933 8.682-19.886 14.842-28.658 17.94ZM50.35 144.747c-12.483-4.267-22.792-9.812-29.858-15.863c-6.35-5.437-9.555-10.836-9.555-15.216c0-9.322 13.897-21.212 37.076-29.293c2.813-.98 5.757-1.905 8.812-2.773c3.204 10.42 7.406 21.315 12.477 32.332c-5.137 11.18-9.399 22.249-12.634 32.792a134.718 134.718 0 0 1-6.318-1.979Zm12.378-84.26c-4.811-24.587-1.616-43.134 6.425-47.789c8.564-4.958 27.502 2.111 47.463 19.835a144.318 144.318 0 0 1 3.841 3.545c-7.438 7.987-14.787 17.08-21.808 26.988c-12.04 1.116-23.565 2.908-34.161 5.309a160.342 160.342 0 0 1-1.76-7.887Zm110.427 27.268a347.8 347.8 0 0 0-7.785-12.803c8.168 1.033 15.994 2.404 23.343 4.08c-2.206 7.072-4.956 14.465-8.193 22.045a381.151 381.151 0 0 0-7.365-13.322Zm-45.032-43.861c5.044 5.465 10.096 11.566 15.065 18.186a322.04 322.04 0 0 0-30.257-.006c4.974-6.559 10.069-12.652 15.192-18.18ZM82.802 87.83a323.167 323.167 0 0 0-7.227 13.238c-3.184-7.553-5.909-14.98-8.134-22.152c7.304-1.634 15.093-2.97 23.209-3.984a321.524 321.524 0 0 0-7.848 12.897Zm8.081 65.352c-8.385-.936-16.291-2.203-23.593-3.793c2.26-7.3 5.045-14.885 8.298-22.6a321.187 321.187 0 0 0 7.257 13.246c2.594 4.48 5.28 8.868 8.038 13.147Zm37.542 31.03c-5.184-5.592-10.354-11.779-15.403-18.433c4.902.192 9.899.29 14.978.29c5.218 0 10.376-.117 15.453-.343c-4.985 6.774-10.018 12.97-15.028 18.486Zm52.198-57.817c3.422 7.8 6.306 15.345 8.596 22.52c-7.422 1.694-15.436 3.058-23.88 4.071a382.417 382.417 0 0 0 7.859-13.026a347.403 347.403 0 0 0 7.425-13.565Zm-16.898 8.101a358.557 358.557 0 0 1-12.281 19.815a329.4 329.4 0 0 1-23.444.823c-7.967 0-15.716-.248-23.178-.732a310.202 310.202 0 0 1-12.513-19.846h.001a307.41 307.41 0 0 1-10.923-20.627a310.278 310.278 0 0 1 10.89-20.637l-.001.001a307.318 307.318 0 0 1 12.413-19.761c7.613-.576 15.42-.876 23.31-.876H128c7.926 0 15.743.303 23.354.883a329.357 329.357 0 0 1 12.335 19.695a358.489 358.489 0 0 1 11.036 20.54a329.472 329.472 0 0 1-11 20.722Zm22.56-122.124c8.572 4.944 11.906 24.881 6.52 51.026c-.344 1.668-.73 3.367-1.15 5.09c-10.622-2.452-22.155-4.275-34.23-5.408c-7.034-10.017-14.323-19.124-21.64-27.008a160.789 160.789 0 0 1 5.888-5.4c18.9-16.447 36.564-22.941 44.612-18.3ZM128 90.808c12.625 0 22.86 10.235 22.86 22.86s-10.235 22.86-22.86 22.86s-22.86-10.235-22.86-22.86s10.235-22.86 22.86-22.86Z"></path></svg>
|
||||||
|
After Width: | Height: | Size: 4.0 KiB |
68
frontend/src/index.css
Normal file
68
frontend/src/index.css
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
:root {
|
||||||
|
font-family: system-ui, Avenir, Helvetica, Arial, sans-serif;
|
||||||
|
line-height: 1.5;
|
||||||
|
font-weight: 400;
|
||||||
|
|
||||||
|
color-scheme: light dark;
|
||||||
|
color: rgba(255, 255, 255, 0.87);
|
||||||
|
background-color: #242424;
|
||||||
|
|
||||||
|
font-synthesis: none;
|
||||||
|
text-rendering: optimizeLegibility;
|
||||||
|
-webkit-font-smoothing: antialiased;
|
||||||
|
-moz-osx-font-smoothing: grayscale;
|
||||||
|
}
|
||||||
|
|
||||||
|
a {
|
||||||
|
font-weight: 500;
|
||||||
|
color: #646cff;
|
||||||
|
text-decoration: inherit;
|
||||||
|
}
|
||||||
|
a:hover {
|
||||||
|
color: #535bf2;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
margin: 0;
|
||||||
|
display: flex;
|
||||||
|
place-items: center;
|
||||||
|
min-width: 320px;
|
||||||
|
min-height: 100vh;
|
||||||
|
}
|
||||||
|
|
||||||
|
h1 {
|
||||||
|
font-size: 3.2em;
|
||||||
|
line-height: 1.1;
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
border-radius: 8px;
|
||||||
|
border: 1px solid transparent;
|
||||||
|
padding: 0.6em 1.2em;
|
||||||
|
font-size: 1em;
|
||||||
|
font-weight: 500;
|
||||||
|
font-family: inherit;
|
||||||
|
background-color: #1a1a1a;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: border-color 0.25s;
|
||||||
|
}
|
||||||
|
button:hover {
|
||||||
|
border-color: #646cff;
|
||||||
|
}
|
||||||
|
button:focus,
|
||||||
|
button:focus-visible {
|
||||||
|
outline: 4px auto -webkit-focus-ring-color;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-color-scheme: light) {
|
||||||
|
:root {
|
||||||
|
color: #213547;
|
||||||
|
background-color: #ffffff;
|
||||||
|
}
|
||||||
|
a:hover {
|
||||||
|
color: #747bff;
|
||||||
|
}
|
||||||
|
button {
|
||||||
|
background-color: #f9f9f9;
|
||||||
|
}
|
||||||
|
}
|
||||||
10
frontend/src/main.tsx
Normal file
10
frontend/src/main.tsx
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { StrictMode } from 'react'
|
||||||
|
import { createRoot } from 'react-dom/client'
|
||||||
|
import './index.css'
|
||||||
|
import App from './App.tsx'
|
||||||
|
|
||||||
|
createRoot(document.getElementById('root')!).render(
|
||||||
|
<StrictMode>
|
||||||
|
<App />
|
||||||
|
</StrictMode>,
|
||||||
|
)
|
||||||
1
frontend/src/vite-env.d.ts
vendored
Normal file
1
frontend/src/vite-env.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/// <reference types="vite/client" />
|
||||||
27
frontend/tsconfig.app.json
Normal file
27
frontend/tsconfig.app.json
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
|
||||||
|
"target": "ES2020",
|
||||||
|
"useDefineForClassFields": true,
|
||||||
|
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
||||||
|
"module": "ESNext",
|
||||||
|
"skipLibCheck": true,
|
||||||
|
|
||||||
|
/* Bundler mode */
|
||||||
|
"moduleResolution": "bundler",
|
||||||
|
"allowImportingTsExtensions": true,
|
||||||
|
"verbatimModuleSyntax": true,
|
||||||
|
"moduleDetection": "force",
|
||||||
|
"noEmit": true,
|
||||||
|
"jsx": "react-jsx",
|
||||||
|
|
||||||
|
/* Linting */
|
||||||
|
"strict": true,
|
||||||
|
"noUnusedLocals": true,
|
||||||
|
"noUnusedParameters": true,
|
||||||
|
"erasableSyntaxOnly": true,
|
||||||
|
"noFallthroughCasesInSwitch": true,
|
||||||
|
"noUncheckedSideEffectImports": true
|
||||||
|
},
|
||||||
|
"include": ["src"]
|
||||||
|
}
|
||||||
7
frontend/tsconfig.json
Normal file
7
frontend/tsconfig.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"files": [],
|
||||||
|
"references": [
|
||||||
|
{ "path": "./tsconfig.app.json" },
|
||||||
|
{ "path": "./tsconfig.node.json" }
|
||||||
|
]
|
||||||
|
}
|
||||||
25
frontend/tsconfig.node.json
Normal file
25
frontend/tsconfig.node.json
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
|
||||||
|
"target": "ES2022",
|
||||||
|
"lib": ["ES2023"],
|
||||||
|
"module": "ESNext",
|
||||||
|
"skipLibCheck": true,
|
||||||
|
|
||||||
|
/* Bundler mode */
|
||||||
|
"moduleResolution": "bundler",
|
||||||
|
"allowImportingTsExtensions": true,
|
||||||
|
"verbatimModuleSyntax": true,
|
||||||
|
"moduleDetection": "force",
|
||||||
|
"noEmit": true,
|
||||||
|
|
||||||
|
/* Linting */
|
||||||
|
"strict": true,
|
||||||
|
"noUnusedLocals": true,
|
||||||
|
"noUnusedParameters": true,
|
||||||
|
"erasableSyntaxOnly": true,
|
||||||
|
"noFallthroughCasesInSwitch": true,
|
||||||
|
"noUncheckedSideEffectImports": true
|
||||||
|
},
|
||||||
|
"include": ["vite.config.ts"]
|
||||||
|
}
|
||||||
8
frontend/vite.config.ts
Normal file
8
frontend/vite.config.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import { defineConfig } from 'vite'
|
||||||
|
import react from '@vitejs/plugin-react-swc'
|
||||||
|
|
||||||
|
|
||||||
|
// https://vite.dev/config/
|
||||||
|
export default defineConfig({
|
||||||
|
plugins: [react()],
|
||||||
|
})
|
||||||
Loading…
x
Reference in New Issue
Block a user