move alot to constants, fix html
Some checks failed
Code quality checks / biome (push) Failing after 12s

This commit is contained in:
creations 2025-06-14 09:19:55 -04:00
parent 92172479f6
commit 33a602cdd0
Signed by: creations
GPG key ID: 8F553AA4320FC711
26 changed files with 603 additions and 296 deletions

View file

@ -1,4 +1,9 @@
import { echo } from "@atums/echo";
import {
errorMessages,
httpStatus,
successMessages,
} from "#environment/constants";
import { sessionManager } from "#lib/auth";
import { cassandra } from "#lib/database";
import { isValidEmail, isValidUsername } from "#lib/validation";
@ -30,22 +35,21 @@ async function handler(
const existingSession = await sessionManager.getSession(request);
if (existingSession) {
const response: LoginResponse = {
code: 409,
code: httpStatus.CONFLICT,
success: false,
error: "User already logged in",
error: errorMessages.USER_ALREADY_LOGGED_IN,
};
return Response.json(response, { status: 409 });
return Response.json(response, { status: httpStatus.CONFLICT });
}
}
if (!identifier || !password) {
const response: LoginResponse = {
code: 400,
code: httpStatus.BAD_REQUEST,
success: false,
error:
"Missing required fields: identifier (username or email), password",
error: errorMessages.MISSING_REQUIRED_FIELDS,
};
return Response.json(response, { status: 400 });
return Response.json(response, { status: httpStatus.BAD_REQUEST });
}
const isEmail = isValidEmail(identifier).valid;
@ -53,11 +57,11 @@ async function handler(
if (!isEmail && !isUsername) {
const response: LoginResponse = {
code: 400,
code: httpStatus.BAD_REQUEST,
success: false,
error: "Invalid identifier format - must be a valid username or email",
};
return Response.json(response, { status: 400 });
return Response.json(response, { status: httpStatus.BAD_REQUEST });
}
let userQuery: string;
@ -83,43 +87,45 @@ async function handler(
if (!userResult?.rows || !Array.isArray(userResult.rows)) {
const response: LoginResponse = {
code: 500,
code: httpStatus.INTERNAL_SERVER_ERROR,
success: false,
error: "Database query failed",
error: errorMessages.DATABASE_QUERY_FAILED,
};
return Response.json(response, { status: 500 });
return Response.json(response, {
status: httpStatus.INTERNAL_SERVER_ERROR,
});
}
if (userResult.rows.length === 0) {
const response: LoginResponse = {
code: 401,
code: httpStatus.UNAUTHORIZED,
success: false,
error: "Invalid credentials",
error: errorMessages.INVALID_CREDENTIALS,
};
return Response.json(response, { status: 401 });
return Response.json(response, { status: httpStatus.UNAUTHORIZED });
}
const user = userResult.rows[0];
if (!user) {
const response: LoginResponse = {
code: 401,
code: httpStatus.UNAUTHORIZED,
success: false,
error: "Invalid credentials",
error: errorMessages.INVALID_CREDENTIALS,
};
return Response.json(response, { status: 401 });
return Response.json(response, { status: httpStatus.UNAUTHORIZED });
}
const isPasswordValid = await Bun.password.verify(password, user.password);
if (!isPasswordValid) {
const response: LoginResponse = {
code: 401,
code: httpStatus.UNAUTHORIZED,
success: false,
error: "Invalid credentials",
error: errorMessages.INVALID_CREDENTIALS,
};
return Response.json(response, { status: 401 });
return Response.json(response, { status: httpStatus.UNAUTHORIZED });
}
const userAgent = request.headers.get("User-Agent") || "Unknown";
@ -148,14 +154,14 @@ async function handler(
};
const response: LoginResponse = {
code: 200,
code: httpStatus.OK,
success: true,
message: "Login successful",
message: successMessages.LOGIN_SUCCESSFUL,
user: responseUser,
};
return Response.json(response, {
status: 200,
status: httpStatus.OK,
headers: {
"Set-Cookie": sessionCookie,
},
@ -167,11 +173,13 @@ async function handler(
});
const response: LoginResponse = {
code: 500,
code: httpStatus.INTERNAL_SERVER_ERROR,
success: false,
error: "Internal server error",
error: errorMessages.INTERNAL_SERVER_ERROR,
};
return Response.json(response, { status: 500 });
return Response.json(response, {
status: httpStatus.INTERNAL_SERVER_ERROR,
});
}
}