All files / src/middleware authenticate.ts

100% Statements 9/9
77.77% Branches 7/9
100% Functions 2/2
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2062x   2x 152x       152x   152x 152x 24x         128x 128x    
import { Request, Response, NextFunction } from "express";
 
export async function authenticate(req: Request, res: Response, next: NextFunction) {
  const db = req.app.locals.database;
 
  // parseInt because we don't want to include "truthy" numbers
  // e.g. hello10 = 10 > this should be NaN
  const sessionId = parseInt(req.cookies.id, 10);
 
  const session = db.auth.sessions.getSession(sessionId);
  if (session == null) {
    return res.status(401).json({
      error: "Unauthorised",
    });
  }
 
  res.session = session;
  return next();
}