Add an option to checkAuth to update the user's latest IP

This commit is contained in:
2026-01-22 18:31:48 -07:00
parent c62d4033d5
commit b4a9d26d0f
9 changed files with 104 additions and 23 deletions

View File

@@ -1,5 +1,9 @@
import { Context } from 'elysia'
import { getDatabaseConnection, jsonResponse } from '../../../../lib/util'
import {
getClientIp,
getDatabaseConnection,
jsonResponse
} from '../../../../lib/util'
import { berryDashUserPosts } from '../../../../lib/tables'
import { and, eq } from 'drizzle-orm'
import { checkAuthorization } from '../../../../lib/bd/auth'
@@ -13,11 +17,19 @@ export async function handler (context: Context) {
{ success: false, message: 'Failed to connect to database', data: null },
500
)
const { connection: connection0, db: db0 } = dbInfo1
const { connection: connection1, db: db1 } = dbInfo1
const ip = getClientIp(context)
const authorizationToken = context.headers.authorization
const authResult = await checkAuthorization(authorizationToken as string, db1)
const authResult = await checkAuthorization(
authorizationToken as string,
db1,
db0,
ip
)
if (!authResult.valid) {
connection0.end()
connection1.end()
return jsonResponse(
{ success: false, message: 'Unauthorized', data: null },
@@ -28,6 +40,7 @@ export async function handler (context: Context) {
let idQuery = context.query.id ? parseInt(context.query.id, 10) : 0
if (!idQuery || idQuery < 1) {
connection0.end()
connection1.end()
return jsonResponse(
{ success: false, message: 'No valid post ID provided', data: null },
@@ -47,6 +60,7 @@ export async function handler (context: Context) {
)
.execute()
connection0.end()
connection1.end()
if (result[0])