-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: rename utils to other because utils doesn't mean anything
- Loading branch information
1 parent
9e444fd
commit 453f484
Showing
24 changed files
with
61 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters