Skip to content

Commit

Permalink
??
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Oct 16, 2023
1 parent 6e2e02a commit 62de25e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
41 changes: 16 additions & 25 deletions src/v2/routes/auth/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,34 @@ const usernameThrottling = new Map<
>()

const LoginSchema = z.object({
username: z
.string({
required_error: "Username is required",
invalid_type_error: "Username must be a string",
})
.min(3, "Username must be at least 3 characters long")
.max(32, "Username must be at most 32 characters long"),
password: z
.string({
required_error: "Password is required",
invalid_type_error: "Password must be a string",
})
.regex(new RegExp(".*[A-Z].*"), "One uppercase character is required")
.regex(new RegExp(".*[a-z].*"), "One lowercase character is required")
.regex(new RegExp(".*\\d.*"), "One number is required")
.regex(
new RegExp(".*[`~<>?,./!@#$%^&*()\\-_+=\"'|{}\\[\\];:\\\\].*"),
"One special character is required"
)
.min(8, "Password must be at least 8 characters long")
.max(128, "Password must be at most 128 characters long"),
username: z.string({
required_error: "Username is required",
invalid_type_error: "Username must be a string",
}),
password: z.string({
required_error: "Password is required",
invalid_type_error: "Password must be a string",
}),
})

export async function login(c: APIContext): Promise<Response> {
const formData = LoginSchema.safeParse(await c.req.formData())
const formData = LoginSchema.safeParse(
await c.req.formData().then((formData) => {
const data = Object.fromEntries(formData.entries())
return data
})
)

if (!formData.success) {
console.log(formData)
return c.json({ success: false, state: "invalid data" }, 400)
}

const { username, password } = formData.data

const validSession = await auth(c.env).handleRequest(c).validate()

if (validSession) {
if (validSession)
return c.json({ success: false, state: "already logged in" }, 200)
}

const storedThrottling = usernameThrottling.get(username)
const timeoutUntil = storedThrottling?.timeoutUntil ?? 0
Expand Down
7 changes: 6 additions & 1 deletion src/v2/routes/auth/signup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ const CreateAccountSchema = z
})

export async function signup(c: APIContext): Promise<Response> {
const formData = CreateAccountSchema.safeParse(await c.req.formData())
const formData = CreateAccountSchema.safeParse(
await c.req.formData().then((formData) => {
const data = Object.fromEntries(formData.entries())
return data
})
)

if (!formData.success) {
return c.json({ success: false, state: "invalid data" }, 400)
Expand Down
7 changes: 7 additions & 0 deletions src/v2/routes/auth/user-attributes/updateUserAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { auth } from "@/v2/lib/auth/lucia"
import { z } from "zod"

type UserAttributes = {
display_name?: string
username?: string
pronouns?: string
self_assignable_role_flags?: number
Expand All @@ -10,6 +11,11 @@ type UserAttributes = {

const UpdateUserAttributesSchema = z
.object({
display_name: z
.string({
invalid_type_error: "Display name must be a string",
})
.optional(),
username: z
.string({
invalid_type_error: "Username must be a string",
Expand Down Expand Up @@ -56,6 +62,7 @@ export async function updateUserAttributes(c: APIContext): Promise<Response> {
}

const attributes: UserAttributes = {
display_name: formData.data.display_name,
username: formData.data.username,
pronouns: formData.data.pronouns,
self_assignable_role_flags: formData.data.self_assignable_roles,
Expand Down
4 changes: 2 additions & 2 deletions src/v2/routes/search/searchRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ authRoute.use(
"/all/:query",
cors({
credentials: true,
origin: ["*"], // TODO: update this - temporary
origin: ["http://localhost:3000"], // TODO: update this - temporary
})
)

authRoute.use(
"/users/user/:username",
cors({
credentials: true,
origin: ["*"], // TODO: update this - temporary
origin: ["http://localhost:3000"], // TODO: update this - temporary
})
)

Expand Down

0 comments on commit 62de25e

Please sign in to comment.