mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-14 07:55:02 +02:00
added auth middleware
This commit is contained in:
parent
7dbf4307e7
commit
942dbd8e56
4 changed files with 44 additions and 0 deletions
|
@ -1,3 +1,4 @@
|
||||||
export * from "./notFound";
|
export * from "./notFound";
|
||||||
export * from "./rateLimit";
|
export * from "./rateLimit";
|
||||||
export * from "./formatError";
|
export * from "./formatError";
|
||||||
|
export * from "./verifySession";
|
||||||
|
|
33
server/middlewares/verifySession.ts
Normal file
33
server/middlewares/verifySession.ts
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
import { NextFunction, Response, Request } from "express";
|
||||||
|
import ErrorResponse from "@server/types/ErrorResponse";
|
||||||
|
import { unauthorized, verifySession } from "@server/auth";
|
||||||
|
import { db } from "@server/db";
|
||||||
|
import { users } from "@server/db/schema";
|
||||||
|
import { eq } from "drizzle-orm";
|
||||||
|
import createHttpError from "http-errors";
|
||||||
|
import HttpCode from "@server/types/HttpCode";
|
||||||
|
|
||||||
|
export const verifySessionMiddleware = async (
|
||||||
|
req: any,
|
||||||
|
res: Response<ErrorResponse>,
|
||||||
|
next: NextFunction,
|
||||||
|
) => {
|
||||||
|
const { session, user } = await verifySession(req);
|
||||||
|
if (!session || !user) {
|
||||||
|
return next(unauthorized());
|
||||||
|
}
|
||||||
|
|
||||||
|
const existingUser = await db
|
||||||
|
.select()
|
||||||
|
.from(users)
|
||||||
|
.where(eq(users.id, user.id));
|
||||||
|
|
||||||
|
if (!existingUser || !existingUser[0]) {
|
||||||
|
return next(
|
||||||
|
createHttpError(HttpCode.BAD_REQUEST, "User does not exist"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
req.user = existingUser[0];
|
||||||
|
req.session = session;
|
||||||
|
};
|
|
@ -6,6 +6,7 @@ import * as target from "./target";
|
||||||
import * as user from "./user";
|
import * as user from "./user";
|
||||||
import * as auth from "./auth";
|
import * as auth from "./auth";
|
||||||
import HttpCode from "@server/types/HttpCode";
|
import HttpCode from "@server/types/HttpCode";
|
||||||
|
import { verifySessionMiddleware } from "@server/middlewares";
|
||||||
|
|
||||||
// Root routes
|
// Root routes
|
||||||
export const unauthenticated = Router();
|
export const unauthenticated = Router();
|
||||||
|
@ -16,6 +17,7 @@ unauthenticated.get("/", (_, res) => {
|
||||||
|
|
||||||
// Authenticated Root routes
|
// Authenticated Root routes
|
||||||
export const authenticated = Router();
|
export const authenticated = Router();
|
||||||
|
authenticated.use(verifySessionMiddleware);
|
||||||
|
|
||||||
authenticated.put("/org", org.createOrg);
|
authenticated.put("/org", org.createOrg);
|
||||||
authenticated.get("/orgs", org.listOrgs);
|
authenticated.get("/orgs", org.listOrgs);
|
||||||
|
|
8
server/types/Auth.ts
Normal file
8
server/types/Auth.ts
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
import { Request } from "express";
|
||||||
|
import { User } from "@server/db/schema";
|
||||||
|
import { Session } from "lucia";
|
||||||
|
|
||||||
|
export interface AuthenticatedRequest extends Request {
|
||||||
|
user: User;
|
||||||
|
session: Session;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue