auth cleanup
This commit is contained in:
@@ -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,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();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
|
||||||
|
|
||||||
export function validateCall(
|
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
) {
|
|
||||||
next();
|
|
||||||
};
|
|
||||||
@@ -51,19 +51,29 @@ authRouter.post("/signup", async (req: Request, res: Response) => {
|
|||||||
.update(`${presalt}${password}${postsalt}`)
|
.update(`${presalt}${password}${postsalt}`)
|
||||||
.digest("base64url");
|
.digest("base64url");
|
||||||
|
|
||||||
await db
|
try {
|
||||||
.insert(users)
|
await db
|
||||||
.values({
|
.insert(users)
|
||||||
display_name: display_name,
|
.values({
|
||||||
username: username,
|
display_name: display_name,
|
||||||
hashed_password: hashedPassword,
|
username: username,
|
||||||
permissions: 44
|
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);
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user