Skip to content

Commit

Permalink
chore: rename utils to other because utils doesn't mean anything
Browse files Browse the repository at this point in the history
  • Loading branch information
yurijmikhalevich committed Oct 5, 2024
1 parent 9e444fd commit 453f484
Show file tree
Hide file tree
Showing 24 changed files with 61 additions and 61 deletions.
2 changes: 1 addition & 1 deletion bin/create-user.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import readline from "readline";
import { Writable } from "stream";
import { ArgumentParser } from "argparse";
import createUser from "../utils/createUser";
import createUser from "../other/createUser";

async function main() {
const parser = new ArgumentParser({
Expand Down
2 changes: 1 addition & 1 deletion components/CodeEditor.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { defaultCode, jsdoc } from "~/utils/defaultCode.js";
import { defaultCode, jsdoc } from "~/other/defaultCode.js";
if (import.meta.client) {
// configure monaco editor on client side
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion utils/statusCodes.ts → other/httpStatusCodes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const STATUS_CODES = {
export const HTTP_STATUS_CODES = {
UNPROCESSABLE_ENTITY: 422,
UNAUTHORIZED: 401,
};
File renamed without changes.
4 changes: 2 additions & 2 deletions utils/prepareBotCode.ts → other/prepareBotCode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { BotCode } from "~/utils/botCodeStore";
import type { WorldState } from "~/utils/world";
import type { BotCode } from "~/other/botCodeStore";
import type { WorldState } from "~/other/world";

type PrepareBotCodeArgs = {
bot: BotCode;
Expand Down
2 changes: 1 addition & 1 deletion utils/recordGamedb.ts → other/recordGamedb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import prisma from "~/utils/db";
import prisma from "~/other/db";

export type GameStat = {
userId: number;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
86 changes: 43 additions & 43 deletions server/api/auth/login.post.ts
Original file line number Diff line number Diff line change
@@ -1,43 +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";
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 });
}
});
import crypto from "crypto";
import bcrypt from "bcrypt";
import { z } from "zod";
import { SESSION_COOKIE_NAME, SESSION_STORAGE, SESSION_TTL_SECONDS } from "~/other/sessionStorage";
import prisma from "~/other/db";
import { HTTP_STATUS_CODES } from "~/other/httpStatusCodes";

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: HTTP_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: HTTP_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: HTTP_STATUS_CODES.UNAUTHORIZED, statusMessage: ERROR_MESSAGES.INVALID_CREDENTIALS });
}
});
2 changes: 1 addition & 1 deletion server/api/auth/logout.post.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SESSION_COOKIE_NAME, SESSION_STORAGE } from "~/utils/sessionStorage";
import { SESSION_COOKIE_NAME, SESSION_STORAGE } from "~/other/sessionStorage";

export default defineEventHandler(async (event) => {
if (!event.context.user) {
Expand Down
2 changes: 1 addition & 1 deletion server/api/auth/user.get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as botCodeStore from "~/utils/botCodeStore";
import * as botCodeStore from "~/other/botCodeStore";

export default defineEventHandler(async (event) => {
if (!event.context.user) {
Expand Down
2 changes: 1 addition & 1 deletion server/api/bot/submit.post.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from "zod";
import * as botCodeStore from "~/utils/botCodeStore";
import * as botCodeStore from "~/other/botCodeStore";

const submitBotCodeSchema = z.object({
code: z.string(),
Expand Down
2 changes: 1 addition & 1 deletion server/api/state.get.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WORLD_REF } from "../plugins/engine";
import * as botCodeStore from "~/utils/botCodeStore";
import * as botCodeStore from "~/other/botCodeStore";

export default defineEventHandler(async () => {
const gameState = WORLD_REF.world.getState();
Expand Down
4 changes: 2 additions & 2 deletions server/middleware/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import prisma from "~/utils/db";
import { SESSION_COOKIE_NAME, SESSION_STORAGE } from "~/utils/sessionStorage";
import prisma from "~/other/db";
import { SESSION_COOKIE_NAME, SESSION_STORAGE } from "~/other/sessionStorage";

export default defineEventHandler(async (event) => {
if (event.path.includes("login")) {
Expand Down
8 changes: 4 additions & 4 deletions server/plugins/engine.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import ivm from "isolated-vm";
import prepareBotCode from "../../utils/prepareBotCode";
import * as botCodeStore from "~/utils/botCodeStore";
import World from "~/utils/world";
import { recordGameEnd, type GameStat } from "~/utils/recordGamedb";
import prepareBotCode from "~/other/prepareBotCode";
import * as botCodeStore from "~/other/botCodeStore";
import World from "~/other/world";
import { recordGameEnd, type GameStat } from "~/other/recordGamedb";

const MEMORY_LIMIT_MB = 64;
const TIME_LIMIT_MS = 75;
Expand Down
4 changes: 2 additions & 2 deletions tests/utils/seed-test-users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import db from "../../utils/db";
import createUser from "../../utils/createUser";
import db from "../../other/db";
import createUser from "../../other/createUser";

const TEST_USERS = [
{ username: "test", password: "1234" },
Expand Down

0 comments on commit 453f484

Please sign in to comment.