auth middleware

This commit is contained in:
2026-05-23 18:03:46 -05:00
parent 3bda27034a
commit 9e645fe06c
3 changed files with 24 additions and 1 deletions
+1
View File
@@ -5,6 +5,7 @@ import { blocksRouter } from './routes/blocks.ts';
import { imagesRouter } from './routes/images.ts';
import { authRouter } from './routes/auth.ts';
import { usersRouter } from './routes/users.ts';
import { auth } from './middleware/auth.ts';
const app = express();
app.use(express.json());
+21
View File
@@ -0,0 +1,21 @@
import { Request, Response, NextFunction } from "express";
import { users } from "../db/schema.ts";
export async function auth(
req: Request,
res: Response,
next: NextFunction
) {
// Either get the user profile of the logged in user,
// or default to anonymous permissions
const token = req.header("Authentication");
if (!token) {
return res.status(401).json({
error: "Missing Token",
});
};
next();
};
+2 -1
View File
@@ -2,11 +2,12 @@ import { drizzle } from 'drizzle-orm/node-postgres';
import { eq } from 'drizzle-orm';
import { users } from '../db/schema.ts';
import express, { Request, Response } from "express";
import { auth } from '../middleware/auth.ts';
const db = drizzle(process.env.DATABASE_URL!);
export const usersRouter = express.Router();
usersRouter.get("/getUser/:userId", async (req: Request, res: Response) => {
usersRouter.get("/getUser/:userId", auth, async (req: Request, res: Response) => {
const { userId } = req.params;
const resp = await db.select()