fosrl.pangolin/server/routers/auth/login.ts

138 lines
3.9 KiB
TypeScript
Raw Normal View History

2024-10-01 20:48:03 -04:00
import { verify } from "@node-rs/argon2";
import lucia from "@server/auth";
import db from "@server/db";
import { users } from "@server/db/schema";
import HttpCode from "@server/types/HttpCode";
import response from "@server/utils/response";
import { eq } from "drizzle-orm";
import { NextFunction, Request, Response } from "express";
import createHttpError from "http-errors";
2024-10-02 20:19:48 -04:00
import { decodeHex } from "oslo/encoding";
import { TOTPController } from "oslo/otp";
import { z } from "zod";
import { fromError } from "zod-validation-error";
2024-10-01 20:48:03 -04:00
export const loginBodySchema = z.object({
email: z.string().email(),
password: z.string(),
2024-10-02 20:19:48 -04:00
code: z.string().optional(),
2024-10-01 20:48:03 -04:00
});
2024-10-02 20:19:48 -04:00
export type LoginBody = z.infer<typeof loginBodySchema>;
export type LoginResponse = {
codeRequested?: boolean;
};
export async function login(
req: Request,
res: Response,
next: NextFunction,
): Promise<any> {
2024-10-01 20:48:03 -04:00
const parsedBody = loginBodySchema.safeParse(req.body);
if (!parsedBody.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
fromError(parsedBody.error).toString(),
),
);
}
2024-10-02 20:19:48 -04:00
const { email, password, code } = parsedBody.data;
2024-10-01 20:48:03 -04:00
const sessionId = req.cookies[lucia.sessionCookieName];
const { session: existingSession } = await lucia.validateSession(sessionId);
if (existingSession) {
return response<null>(res, {
data: null,
success: true,
error: false,
message: "Already logged in",
status: HttpCode.OK,
});
}
2024-10-01 20:48:03 -04:00
const existingUserRes = await db
.select()
.from(users)
.where(eq(users.email, email));
if (!existingUserRes || !existingUserRes.length) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
"A user with that email address does not exist",
),
);
}
const existingUser = existingUserRes[0];
const validPassword = await verify(existingUser.passwordHash, password, {
memoryCost: 19456,
timeCost: 2,
outputLen: 32,
parallelism: 1,
});
if (!validPassword) {
2024-10-02 20:19:48 -04:00
await new Promise((resolve) => setTimeout(resolve, 250)); // delay to prevent brute force attacks
2024-10-01 20:48:03 -04:00
return next(
createHttpError(
HttpCode.BAD_REQUEST,
"The password you entered is incorrect",
),
);
}
2024-10-02 20:19:48 -04:00
if (existingUser.twoFactorEnabled) {
if (!code) {
return response<{ codeRequested: boolean }>(res, {
data: { codeRequested: true },
success: true,
error: false,
message: "Two-factor authentication required",
status: HttpCode.ACCEPTED,
});
2024-10-02 20:19:48 -04:00
}
if (!existingUser.twoFactorSecret) {
return next(
createHttpError(
HttpCode.INTERNAL_SERVER_ERROR,
"Failed to authenticate user",
),
);
}
const validOTP = await new TOTPController().verify(
code,
decodeHex(existingUser.twoFactorSecret),
);
if (!validOTP) {
await new Promise((resolve) => setTimeout(resolve, 250)); // delay to prevent brute force attacks
return next(
createHttpError(
HttpCode.BAD_REQUEST,
"The two-factor code you entered is incorrect",
),
);
}
}
2024-10-01 20:48:03 -04:00
const session = await lucia.createSession(existingUser.id, {});
res.appendHeader(
"Set-Cookie",
lucia.createSessionCookie(session.id).serialize(),
);
return response<null>(res, {
data: null,
success: true,
error: false,
message: "Logged in successfully",
status: HttpCode.OK,
});
2024-10-01 20:48:03 -04:00
}