Change ussername & password endpoints

This commit is contained in:
2026-01-23 22:07:34 -07:00
parent 3dd5d34095
commit 8400dfe9cb
3 changed files with 215 additions and 28 deletions

View File

@@ -0,0 +1,80 @@
import { Context } from 'elysia'
import {
getClientIp,
getDatabaseConnection,
jsonResponse
} from '../../../../lib/util'
import { checkAuthorization } from '../../../../lib/bd/auth'
import { users } from '../../../../lib/tables'
import { eq } from 'drizzle-orm'
import bcrypt from 'bcryptjs'
type Body = {
newPassword: string
}
export async function handler (context: Context) {
const dbInfo0 = getDatabaseConnection(0)
const dbInfo1 = getDatabaseConnection(1)
if (!dbInfo0 || !dbInfo1)
return jsonResponse(
{ success: false, message: 'Failed to connect to database' },
500
)
const { connection: connection0, db: db0 } = dbInfo0
const { connection: connection1, db: db1 } = dbInfo1
const ip = getClientIp(context)
const authorizationToken = context.headers.authorization
const authResult = await checkAuthorization(
authorizationToken as string,
db1,
db0,
ip
)
if (!authResult.valid) {
connection0.end()
connection1.end()
return jsonResponse({ success: false, message: 'Unauthorized' }, 401)
}
const userId = authResult.id
const body = context.body as Body
if (!body.newPassword) {
connection0.end()
connection1.end()
return jsonResponse(
{ success: false, message: 'No new password provided' },
400
)
}
if (
!/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d!@#$%^&*()_\-+=]{8,}$/.test(
body.newPassword
)
) {
connection0.end()
connection1.end()
return jsonResponse(
{
success: false,
message:
'New password must be at least 8 characters with at least one letter and one number',
data: null
},
400
)
}
const hashedPassword = await bcrypt.hash(body.newPassword, 10)
await db0
.update(users)
.set({ password: hashedPassword })
.where(eq(users.id, userId))
.execute()
return jsonResponse({ success: true, message: null })
}

View File

@@ -0,0 +1,73 @@
import { Context } from 'elysia'
import {
getClientIp,
getDatabaseConnection,
jsonResponse
} from '../../../../lib/util'
import { checkAuthorization } from '../../../../lib/bd/auth'
import { users } from '../../../../lib/tables'
import { eq } from 'drizzle-orm'
type Body = {
newUsername: string
}
export async function handler (context: Context) {
const dbInfo0 = getDatabaseConnection(0)
const dbInfo1 = getDatabaseConnection(1)
if (!dbInfo0 || !dbInfo1)
return jsonResponse(
{ success: false, message: 'Failed to connect to database' },
500
)
const { connection: connection0, db: db0 } = dbInfo0
const { connection: connection1, db: db1 } = dbInfo1
const ip = getClientIp(context)
const authorizationToken = context.headers.authorization
const authResult = await checkAuthorization(
authorizationToken as string,
db1,
db0,
ip
)
if (!authResult.valid) {
connection0.end()
connection1.end()
return jsonResponse({ success: false, message: 'Unauthorized' }, 401)
}
const userId = authResult.id
const body = context.body as Body
if (!body.newUsername) {
connection0.end()
connection1.end()
return jsonResponse(
{ success: false, message: 'No new username provided' },
400
)
}
if (!/^[a-zA-Z0-9]{3,16}$/.test(body.newUsername)) {
connection0.end()
connection1.end()
return jsonResponse(
{
success: false,
message:
'New username must be 3-16 characters, letters and numbers only',
data: null
},
400
)
}
await db0
.update(users)
.set({ username: body.newUsername })
.where(eq(users.id, userId))
.execute()
return jsonResponse({ success: true, message: null })
}