added authentication logging
This commit is contained in:
@@ -6,9 +6,10 @@ import {TournamentService} from './services/tournament-service';
|
|||||||
import {UserService} from './services/user-service';
|
import {UserService} from './services/user-service';
|
||||||
import {TeamService} from './services/team-service';
|
import {TeamService} from './services/team-service';
|
||||||
import {authMiddleware} from './middlewares/auth-middleware';
|
import {authMiddleware} from './middlewares/auth-middleware';
|
||||||
import router from './middlewares/logger';
|
import loggingMiddleware from './middlewares/logger';
|
||||||
import {Database} from 'sqlite3';
|
import {Database} from 'sqlite3';
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
import { log } from 'console';
|
||||||
|
|
||||||
|
|
||||||
const dbFilename = 'tournaments.sqlite';
|
const dbFilename = 'tournaments.sqlite';
|
||||||
@@ -24,7 +25,7 @@ const port = process.env.PORT || 3000;
|
|||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
app.use(router);
|
app.use(loggingMiddleware);
|
||||||
|
|
||||||
app.get('/tournaments', async (req: Request, res: Response) => {
|
app.get('/tournaments', async (req: Request, res: Response) => {
|
||||||
const tournaments = await tournamentService.getAllTournaments();
|
const tournaments = await tournamentService.getAllTournaments();
|
||||||
|
|||||||
@@ -10,9 +10,10 @@ export const authMiddleware = (req: Request, res: Response, next: NextFunction)
|
|||||||
}
|
}
|
||||||
const token = authHeader.split(' ')[1];
|
const token = authHeader.split(' ')[1];
|
||||||
try {
|
try {
|
||||||
const decoded = jwt.verify(token, JWT_SECRET);
|
const decoded = jwt.verify(token, JWT_SECRET) as jwt.JwtPayload;
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
req.user = decoded;
|
req.user = decoded;
|
||||||
|
console.log("User authenticated:", decoded.username);
|
||||||
|
|
||||||
next();
|
next();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -7,11 +7,11 @@ const filePath = path.join(process.cwd(), 'request_logs.txt');
|
|||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
router.use((req, res, next) => {
|
router.use((req, res, next) => {
|
||||||
const log = `[${new Date().toLocaleString()}] ${req.method} ${req.url}\n`;
|
const log = `\n[${new Date().toLocaleString()}] ${req.method} ${req.url}`;
|
||||||
console.log(log);
|
console.log(log);
|
||||||
|
|
||||||
fs.appendFile(filePath, log, (err) => {
|
fs.appendFile(filePath, log, (err) => {
|
||||||
if (err) console.error("Request Failed", err);
|
if (err) console.error("Writing to log failed", err);
|
||||||
});
|
});
|
||||||
|
|
||||||
next();
|
next();
|
||||||
|
|||||||
Reference in New Issue
Block a user