auth stuff
This commit is contained in:
@@ -1,21 +1,26 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, NextFunction } from "express";
|
||||||
|
import { drizzle } from 'drizzle-orm/node-postgres';
|
||||||
import { users } from "../db/schema.ts";
|
import { users } from "../db/schema.ts";
|
||||||
|
import { eq } from 'drizzle-orm';
|
||||||
|
|
||||||
|
const db = drizzle(process.env.DATABASE_URL!);
|
||||||
|
|
||||||
export async function auth(
|
export async function auth(
|
||||||
req: Request,
|
req: Request,
|
||||||
res: Response,
|
res: Response,
|
||||||
next: NextFunction
|
next: NextFunction
|
||||||
) {
|
) {
|
||||||
// Either get the user profile of the logged in user,
|
// read/decode jwt
|
||||||
// or default to anonymous permissions
|
|
||||||
|
|
||||||
const token = req.header("Authentication");
|
|
||||||
|
|
||||||
if (!token) {
|
|
||||||
return res.status(401).json({
|
|
||||||
error: "Missing Token",
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
async function getUserPermissions(userId: string) {
|
||||||
|
const resp = await db
|
||||||
|
.select({ perms: users.permissions })
|
||||||
|
.from(users)
|
||||||
|
.where(eq(users.id, userId));
|
||||||
|
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ 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", auth, async (req: Request, res: Response) => {
|
usersRouter.get("/getUser/:userId", 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