|
@@ -1,3 +1,4 @@
|
|
|
|
+/* eslint-disable no-unused-vars */
|
|
import express, { NextFunction, Request, Response } from 'express';
|
|
import express, { NextFunction, Request, Response } from 'express';
|
|
import compression from 'compression';
|
|
import compression from 'compression';
|
|
// import suExec from 'su-exec';
|
|
// import suExec from 'su-exec';
|
|
@@ -6,7 +7,9 @@ import cors from 'cors';
|
|
import { isProd } from './constants/constants';
|
|
import { isProd } from './constants/constants';
|
|
import appsRoutes from './modules/apps/apps.routes';
|
|
import appsRoutes from './modules/apps/apps.routes';
|
|
import systemRoutes from './modules/system/system.routes';
|
|
import systemRoutes from './modules/system/system.routes';
|
|
-import networkRoutes from './modules/network/network.routes';
|
|
|
|
|
|
+import authRoutes from './modules/auth/auth.routes';
|
|
|
|
+import { tradeTokenForUser } from './modules/auth/auth.helpers';
|
|
|
|
+import cookieParser from 'cookie-parser';
|
|
|
|
|
|
// suExec.init();
|
|
// suExec.init();
|
|
|
|
|
|
@@ -14,6 +17,7 @@ const app = express();
|
|
const port = 3001;
|
|
const port = 3001;
|
|
|
|
|
|
app.use(express.json());
|
|
app.use(express.json());
|
|
|
|
+app.use(cookieParser());
|
|
|
|
|
|
if (isProd) {
|
|
if (isProd) {
|
|
app.use(compression());
|
|
app.use(compression());
|
|
@@ -22,12 +26,32 @@ if (isProd) {
|
|
|
|
|
|
app.use(cors());
|
|
app.use(cors());
|
|
|
|
|
|
-app.use('/system', systemRoutes);
|
|
|
|
-app.use('/apps', appsRoutes);
|
|
|
|
-app.use('/network', networkRoutes);
|
|
|
|
|
|
+// Get user from token
|
|
|
|
+app.use((req, res, next) => {
|
|
|
|
+ let user = null;
|
|
|
|
+
|
|
|
|
+ if (req?.cookies?.tipi_token) {
|
|
|
|
+ user = tradeTokenForUser(req.cookies.tipi_token);
|
|
|
|
+ if (user) req.user = user;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ next();
|
|
|
|
+});
|
|
|
|
+
|
|
|
|
+const restrict = (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
+ if (!req.user) {
|
|
|
|
+ res.status(401).json({ error: 'Unauthorized' });
|
|
|
|
+ } else {
|
|
|
|
+ next();
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+app.use('/auth', authRoutes);
|
|
|
|
+app.use('/system', restrict, systemRoutes);
|
|
|
|
+app.use('/apps', restrict, appsRoutes);
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
-app.use((err: Error, req: Request, res: Response, _next: NextFunction) => {
|
|
|
|
|
|
+app.use((err: Error, req: Request, res: Response, _: NextFunction) => {
|
|
res.status(200).json({ error: err.message });
|
|
res.status(200).json({ error: err.message });
|
|
});
|
|
});
|
|
|
|
|