Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add http status code constants #51

Merged
merged 9 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 43 additions & 37 deletions server/api/auth/login.post.ts
yurijmikhalevich marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
import crypto from "crypto";
import bcrypt from "bcrypt";
import { z } from "zod";
import { SESSION_COOKIE_NAME, SESSION_STORAGE, SESSION_TTL_SECONDS } from "~/utils/sessionStorage";
import prisma from "~/utils/db";

const userSchema = z.object({
username: z.string(),
password: z.string(),
});

export default defineEventHandler(async (event) => {
const result = await readValidatedBody(event, body => userSchema.safeParse(body));
if (!result.success) {
throw createError({ statusCode: 422, statusMessage: "Username and password field is required" });
}

const user = await prisma.user.findFirst({ where: { username: result.data.username } });
if (!user) {
throw createError({ statusCode: 401, statusMessage: `Unauthorized` });
}

const hash = user.password;
const match = await bcrypt.compare(result.data.password, hash);
if (match) {
const sessionId = crypto.randomUUID();
setCookie(event, SESSION_COOKIE_NAME, sessionId, {
httpOnly: true,
secure: true,
maxAge: SESSION_TTL_SECONDS,
});
SESSION_STORAGE.set(sessionId, user.id);
return sendRedirect(event, "/");
} else {
throw createError({ statusCode: 401, statusMessage: "Invalid password or username" });
}
});
import crypto from "crypto";
import bcrypt from "bcrypt";
import { z } from "zod";
import { SESSION_COOKIE_NAME, SESSION_STORAGE, SESSION_TTL_SECONDS } from "~/utils/sessionStorage";
import prisma from "~/utils/db";
import { STATUS_CODES } from "~/utils/statusCodes";

const ERROR_MESSAGES = {
VALIDATION_ERROR: "Username and password field is required",
USER_NOT_FOUND: "Invalid username or password",
INVALID_CREDENTIALS: "Invalid username or password",
};

const userSchema = z.object({
username: z.string(),
password: z.string(),
});

export default defineEventHandler(async (event) => {
const result = await readValidatedBody(event, body => userSchema.safeParse(body));
if (!result.success) {
throw createError({ statusCode: STATUS_CODES.UNPROCESSABLE_ENTITY, statusMessage: ERROR_MESSAGES.VALIDATION_ERROR });
}

const user = await prisma.user.findFirst({ where: { username: result.data.username } });
if (!user) {
throw createError({ statusCode: STATUS_CODES.UNAUTHORIZED, statusMessage: ERROR_MESSAGES.USER_NOT_FOUND });
}

const isPasswordValid = await bcrypt.compare(result.data.password, user.password);
if (isPasswordValid) {
const sessionId = crypto.randomUUID();
setCookie(event, SESSION_COOKIE_NAME, sessionId, {
httpOnly: true,
secure: true,
maxAge: SESSION_TTL_SECONDS,
});
SESSION_STORAGE.set(sessionId, user.id);
return sendRedirect(event, "/");
} else {
throw createError({ statusCode: STATUS_CODES.UNAUTHORIZED, statusMessage: ERROR_MESSAGES.INVALID_CREDENTIALS });
}
});
4 changes: 4 additions & 0 deletions utils/statusCodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const STATUS_CODES = {
UNPROCESSABLE_ENTITY: 422,
UNAUTHORIZED: 401,
};