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