From e9e4d8b51ddc28d974ce961574d2b87a6587bb20 Mon Sep 17 00:00:00 2001 From: ModZero Date: Mon, 7 Oct 2019 23:51:44 +0200 Subject: [PATCH] Middleware to add an auth requirement --- src/auth.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/auth.ts b/src/auth.ts index a714457..9dad48b 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -16,6 +16,7 @@ import { db } from "@kredens/db"; import { User } from "@kredens/db/models"; import express from "express"; +import createHttpError from "http-errors"; import { None } from "monet"; export const getUser = async (req: express.Request) => @@ -38,3 +39,11 @@ export const authMiddleware: () => express.Handler = () => async ( next(); }; + +export const requireAuthMiddleware: express.Handler = (req, res, next) => { + if (!req.user) { + next(createHttpError(401)); + } + + next(); +};