auth middleware
This commit is contained in:
@@ -5,6 +5,7 @@ import { blocksRouter } from './routes/blocks.ts';
|
|||||||
import { imagesRouter } from './routes/images.ts';
|
import { imagesRouter } from './routes/images.ts';
|
||||||
import { authRouter } from './routes/auth.ts';
|
import { authRouter } from './routes/auth.ts';
|
||||||
import { usersRouter } from './routes/users.ts';
|
import { usersRouter } from './routes/users.ts';
|
||||||
|
import { auth } from './middleware/auth.ts';
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|||||||
@@ -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,11 +2,12 @@ import { drizzle } from 'drizzle-orm/node-postgres';
|
|||||||
import { eq } from 'drizzle-orm';
|
import { eq } from 'drizzle-orm';
|
||||||
import { users } from '../db/schema.ts';
|
import { users } from '../db/schema.ts';
|
||||||
import express, { Request, Response } from "express";
|
import express, { Request, Response } from "express";
|
||||||
|
import { auth } from '../middleware/auth.ts';
|
||||||
|
|
||||||
const db = drizzle(process.env.DATABASE_URL!);
|
const db = drizzle(process.env.DATABASE_URL!);
|
||||||
export const usersRouter = express.Router();
|
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 { userId } = req.params;
|
||||||
|
|
||||||
const resp = await db.select()
|
const resp = await db.select()
|
||||||
|
|||||||
Reference in New Issue
Block a user