Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions mern/server/routes/record.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import express from "express";
import crypto from "crypto";

// This will help us connect to the database
import db from "../db/connection.js";
Expand All @@ -11,6 +12,23 @@ import { ObjectId } from "mongodb";
// The router will be added as a middleware and will take control of requests starting with path /record.
const router = express.Router();

// Require a valid API key on every request to this router so that record
// data cannot be read or modified by unauthenticated clients.
router.use((req, res, next) => {
const expectedKey = process.env.API_KEY;
const providedKey = req.header("x-api-key") || "";

if (
!expectedKey ||
providedKey.length !== expectedKey.length ||
!crypto.timingSafeEqual(Buffer.from(providedKey), Buffer.from(expectedKey))
) {
return res.status(401).send("Unauthorized");
}

next();
});

// This section will help you get a list of all the records.
router.get("/", async (req, res) => {
let collection = await db.collection("records");
Expand Down