auth cleanup
This commit is contained in:
@@ -2,7 +2,6 @@ import { Request, Response, NextFunction } from "express";
|
||||
import JsonWebToken from '../types/JsonWebToken.ts';
|
||||
import { verifySignature, decodeJWT } from '../helpers/jwt.ts';
|
||||
import { refreshJWT } from "./refreshJWT.ts";
|
||||
import { validateCall } from "./validateCall.ts";
|
||||
|
||||
export function loadJWT(
|
||||
req: Request,
|
||||
@@ -68,6 +67,6 @@ export function loadJWT(
|
||||
}
|
||||
}
|
||||
|
||||
return validateCall(req, res, next);
|
||||
return next();
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import { validateCall } from "./validateCall.ts";
|
||||
import { drizzle } from 'drizzle-orm/node-postgres';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { users } from '../db/schema.ts';
|
||||
@@ -66,5 +65,5 @@ export async function refreshJWT(
|
||||
const parsedJWT = decodeJWT(jwt);
|
||||
req.jwt = parsedJWT;
|
||||
|
||||
return validateCall(req, res, next);
|
||||
return next();
|
||||
};
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
|
||||
export function validateCall(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) {
|
||||
next();
|
||||
};
|
||||
@@ -51,6 +51,7 @@ authRouter.post("/signup", async (req: Request, res: Response) => {
|
||||
.update(`${presalt}${password}${postsalt}`)
|
||||
.digest("base64url");
|
||||
|
||||
try {
|
||||
await db
|
||||
.insert(users)
|
||||
.values({
|
||||
@@ -59,11 +60,20 @@ authRouter.post("/signup", async (req: Request, res: Response) => {
|
||||
hashed_password: hashedPassword,
|
||||
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);
|
||||
});
|
||||
|
||||
// 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) {
|
||||
const hashedPassword = crypto
|
||||
.createHmac('sha256', hashSecret)
|
||||
|
||||
Reference in New Issue
Block a user