auth cleanup

This commit is contained in:
2026-07-04 00:28:31 -05:00
parent 4149e13337
commit 1e43be14bd
4 changed files with 21 additions and 22 deletions
+1 -2
View File
@@ -2,7 +2,6 @@ import { Request, Response, NextFunction } from "express";
import JsonWebToken from '../types/JsonWebToken.ts'; import JsonWebToken from '../types/JsonWebToken.ts';
import { verifySignature, decodeJWT } from '../helpers/jwt.ts'; import { verifySignature, decodeJWT } from '../helpers/jwt.ts';
import { refreshJWT } from "./refreshJWT.ts"; import { refreshJWT } from "./refreshJWT.ts";
import { validateCall } from "./validateCall.ts";
export function loadJWT( export function loadJWT(
req: Request, req: Request,
@@ -68,6 +67,6 @@ export function loadJWT(
} }
} }
return validateCall(req, res, next); return next();
}; };
+1 -2
View File
@@ -1,5 +1,4 @@
import { Request, Response, NextFunction } from "express"; import { Request, Response, NextFunction } from "express";
import { validateCall } from "./validateCall.ts";
import { drizzle } from 'drizzle-orm/node-postgres'; 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';
@@ -66,5 +65,5 @@ export async function refreshJWT(
const parsedJWT = decodeJWT(jwt); const parsedJWT = decodeJWT(jwt);
req.jwt = parsedJWT; req.jwt = parsedJWT;
return validateCall(req, res, next); return next();
}; };
-9
View File
@@ -1,9 +0,0 @@
import { Request, Response, NextFunction } from "express";
export function validateCall(
req: Request,
res: Response,
next: NextFunction
) {
next();
};
+11 -1
View File
@@ -51,6 +51,7 @@ authRouter.post("/signup", async (req: Request, res: Response) => {
.update(`${presalt}${password}${postsalt}`) .update(`${presalt}${password}${postsalt}`)
.digest("base64url"); .digest("base64url");
try {
await db await db
.insert(users) .insert(users)
.values({ .values({
@@ -59,11 +60,20 @@ authRouter.post("/signup", async (req: Request, res: Response) => {
hashed_password: hashedPassword, hashed_password: hashedPassword,
permissions: 44 permissions: 44
}); });
} catch (err: any) {
if (err.cause.code === "23505") {
return res.status(409).json({
error: "Username already exists"
});
}
throw err;
}
loginUser(username, password, res); loginUser(username, password, res);
}); });
// because it write the response, has to be last step // because it writes to the response, has to be last step
async function loginUser(username: string, password: string, res: Response) { async function loginUser(username: string, password: string, res: Response) {
const hashedPassword = crypto const hashedPassword = crypto
.createHmac('sha256', hashSecret) .createHmac('sha256', hashSecret)