Add an option to checkAuth to update the user's latest IP
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
import { MySql2Database } from 'drizzle-orm/mysql2'
|
import { MySql2Database } from 'drizzle-orm/mysql2'
|
||||||
import { berryDashUserData } from '../tables'
|
import { berryDashUserData, users } from '../tables'
|
||||||
import { eq } from 'drizzle-orm'
|
import { eq } from 'drizzle-orm'
|
||||||
|
|
||||||
export async function checkAuthorization (
|
export async function checkAuthorization (
|
||||||
authorizationToken: string,
|
authorizationToken: string,
|
||||||
db1: MySql2Database
|
db1: MySql2Database,
|
||||||
|
db0?: MySql2Database,
|
||||||
|
updateIp?: string | null
|
||||||
) {
|
) {
|
||||||
if (!authorizationToken) return { valid: false, id: 0 }
|
if (!authorizationToken) return { valid: false, id: 0 }
|
||||||
|
|
||||||
@@ -15,5 +17,14 @@ export async function checkAuthorization (
|
|||||||
.execute()
|
.execute()
|
||||||
|
|
||||||
if (!userData[0]) return { valid: false, id: 0 }
|
if (!userData[0]) return { valid: false, id: 0 }
|
||||||
else return { valid: true, id: userData[0].id }
|
else {
|
||||||
|
if (updateIp != undefined && updateIp != null && db0 != undefined)
|
||||||
|
db0
|
||||||
|
.update(users)
|
||||||
|
.set({ latestIp: updateIp })
|
||||||
|
.where(eq(users.id, userData[0].id))
|
||||||
|
.execute()
|
||||||
|
|
||||||
|
return { valid: true, id: userData[0].id }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
import { Context } from 'elysia'
|
import { Context } from 'elysia'
|
||||||
import { getDatabaseConnection, jsonResponse } from '../../../../lib/util'
|
import {
|
||||||
|
getClientIp,
|
||||||
|
getDatabaseConnection,
|
||||||
|
jsonResponse
|
||||||
|
} from '../../../../lib/util'
|
||||||
import { checkAuthorization } from '../../../../lib/bd/auth'
|
import { checkAuthorization } from '../../../../lib/bd/auth'
|
||||||
import { berryDashUserData, users } from '../../../../lib/tables'
|
import { berryDashUserData, users } from '../../../../lib/tables'
|
||||||
import { eq } from 'drizzle-orm'
|
import { eq } from 'drizzle-orm'
|
||||||
@@ -16,8 +20,14 @@ export async function handler (context: Context) {
|
|||||||
const { connection: connection0, db: db0 } = dbInfo0
|
const { connection: connection0, db: db0 } = dbInfo0
|
||||||
const { connection: connection1, db: db1 } = dbInfo1
|
const { connection: connection1, db: db1 } = dbInfo1
|
||||||
|
|
||||||
|
const ip = getClientIp(context)
|
||||||
const authorizationToken = context.headers.authorization
|
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) {
|
if (!authResult.valid) {
|
||||||
connection0.end()
|
connection0.end()
|
||||||
connection1.end()
|
connection1.end()
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
import { Context } from 'elysia'
|
import { Context } from 'elysia'
|
||||||
import { getDatabaseConnection, jsonResponse } from '../../../../lib/util'
|
import {
|
||||||
|
getClientIp,
|
||||||
|
getDatabaseConnection,
|
||||||
|
jsonResponse
|
||||||
|
} from '../../../../lib/util'
|
||||||
import { checkAuthorization } from '../../../../lib/bd/auth'
|
import { checkAuthorization } from '../../../../lib/bd/auth'
|
||||||
import { berryDashUserData } from '../../../../lib/tables'
|
import { berryDashUserData } from '../../../../lib/tables'
|
||||||
import { eq } from 'drizzle-orm'
|
import { eq } from 'drizzle-orm'
|
||||||
@@ -20,8 +24,14 @@ export async function handler (context: Context) {
|
|||||||
const { connection: connection0, db: db0 } = dbInfo0
|
const { connection: connection0, db: db0 } = dbInfo0
|
||||||
const { connection: connection1, db: db1 } = dbInfo1
|
const { connection: connection1, db: db1 } = dbInfo1
|
||||||
|
|
||||||
|
const ip = getClientIp(context)
|
||||||
const authorizationToken = context.headers.authorization
|
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) {
|
if (!authResult.valid) {
|
||||||
connection0.end()
|
connection0.end()
|
||||||
connection1.end()
|
connection1.end()
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
import { Context } from 'elysia'
|
import { Context } from 'elysia'
|
||||||
import { getDatabaseConnection, jsonResponse } from '../../../lib/util'
|
import {
|
||||||
|
getClientIp,
|
||||||
|
getDatabaseConnection,
|
||||||
|
jsonResponse
|
||||||
|
} from '../../../lib/util'
|
||||||
import { berryDashMarketplaceIcons, users } from '../../../lib/tables'
|
import { berryDashMarketplaceIcons, users } from '../../../lib/tables'
|
||||||
import { and, eq, inArray, or, sql, not } from 'drizzle-orm'
|
import { and, eq, inArray, or, sql, not } from 'drizzle-orm'
|
||||||
import { checkAuthorization } from '../../../lib/bd/auth'
|
import { checkAuthorization } from '../../../lib/bd/auth'
|
||||||
@@ -41,8 +45,14 @@ export async function handler (context: Context) {
|
|||||||
const { connection: connection0, db: db0 } = dbInfo0
|
const { connection: connection0, db: db0 } = dbInfo0
|
||||||
const { connection: connection1, db: db1 } = dbInfo1
|
const { connection: connection1, db: db1 } = dbInfo1
|
||||||
|
|
||||||
|
const ip = getClientIp(context)
|
||||||
const authorizationToken = context.headers.authorization
|
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) {
|
if (!authResult.valid) {
|
||||||
connection0.end()
|
connection0.end()
|
||||||
connection1.end()
|
connection1.end()
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import { launcherVersionManifest } from '../../../lib/tables'
|
import { launcherVersionManifest } from '../../../lib/tables'
|
||||||
import { and, desc, eq } from 'drizzle-orm'
|
import { and, desc, eq } from 'drizzle-orm'
|
||||||
import { getDatabaseConnection, jsonResponse } from '../../../lib/util'
|
import { getDatabaseConnection, jsonResponse } from '../../../lib/util'
|
||||||
import { Context } from 'elysia'
|
|
||||||
|
|
||||||
export async function handler (context: Context) {
|
export async function handler () {
|
||||||
const dbResult = getDatabaseConnection(0)
|
const dbResult = getDatabaseConnection(0)
|
||||||
if (!dbResult)
|
if (!dbResult)
|
||||||
return jsonResponse({ error: 'Failed to connect to database' }, 500)
|
return jsonResponse({ error: 'Failed to connect to database' }, 500)
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
import { Context } from 'elysia'
|
import { Context } from 'elysia'
|
||||||
import { getDatabaseConnection, jsonResponse } from '../../../../lib/util'
|
import {
|
||||||
|
getClientIp,
|
||||||
|
getDatabaseConnection,
|
||||||
|
jsonResponse
|
||||||
|
} from '../../../../lib/util'
|
||||||
import { berryDashUserPosts } from '../../../../lib/tables'
|
import { berryDashUserPosts } from '../../../../lib/tables'
|
||||||
import { and, eq } from 'drizzle-orm'
|
import { and, eq } from 'drizzle-orm'
|
||||||
import { checkAuthorization } from '../../../../lib/bd/auth'
|
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 },
|
{ success: false, message: 'Failed to connect to database', data: null },
|
||||||
500
|
500
|
||||||
)
|
)
|
||||||
|
const { connection: connection0, db: db0 } = dbInfo1
|
||||||
const { connection: connection1, db: db1 } = dbInfo1
|
const { connection: connection1, db: db1 } = dbInfo1
|
||||||
|
|
||||||
|
const ip = getClientIp(context)
|
||||||
const authorizationToken = context.headers.authorization
|
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) {
|
if (!authResult.valid) {
|
||||||
|
connection0.end()
|
||||||
connection1.end()
|
connection1.end()
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
{ success: false, message: 'Unauthorized', data: null },
|
{ 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
|
let idQuery = context.query.id ? parseInt(context.query.id, 10) : 0
|
||||||
if (!idQuery || idQuery < 1) {
|
if (!idQuery || idQuery < 1) {
|
||||||
|
connection0.end()
|
||||||
connection1.end()
|
connection1.end()
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
{ success: false, message: 'No valid post ID provided', data: null },
|
{ success: false, message: 'No valid post ID provided', data: null },
|
||||||
@@ -47,6 +60,7 @@ export async function handler (context: Context) {
|
|||||||
)
|
)
|
||||||
.execute()
|
.execute()
|
||||||
|
|
||||||
|
connection0.end()
|
||||||
connection1.end()
|
connection1.end()
|
||||||
|
|
||||||
if (result[0])
|
if (result[0])
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
import { Context } from 'elysia'
|
import { Context } from 'elysia'
|
||||||
import { getDatabaseConnection, jsonResponse } from '../../../../lib/util'
|
import {
|
||||||
|
getClientIp,
|
||||||
|
getDatabaseConnection,
|
||||||
|
jsonResponse
|
||||||
|
} from '../../../../lib/util'
|
||||||
import { berryDashUserPosts } from '../../../../lib/tables'
|
import { berryDashUserPosts } from '../../../../lib/tables'
|
||||||
import { checkAuthorization } from '../../../../lib/bd/auth'
|
import { checkAuthorization } from '../../../../lib/bd/auth'
|
||||||
|
|
||||||
@@ -16,11 +20,19 @@ export async function handler (context: Context) {
|
|||||||
{ success: false, message: 'Failed to connect to database', data: null },
|
{ success: false, message: 'Failed to connect to database', data: null },
|
||||||
500
|
500
|
||||||
)
|
)
|
||||||
|
const { connection: connection0, db: db0 } = dbInfo1
|
||||||
const { connection: connection1, db: db1 } = dbInfo1
|
const { connection: connection1, db: db1 } = dbInfo1
|
||||||
|
|
||||||
|
const ip = getClientIp(context)
|
||||||
const authorizationToken = context.headers.authorization
|
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) {
|
if (!authResult.valid) {
|
||||||
|
connection0.end()
|
||||||
connection1.end()
|
connection1.end()
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
{ success: false, message: 'Unauthorized', data: null },
|
{ success: false, message: 'Unauthorized', data: null },
|
||||||
@@ -31,6 +43,7 @@ export async function handler (context: Context) {
|
|||||||
|
|
||||||
const body = context.body as Body
|
const body = context.body as Body
|
||||||
if (!body.content) {
|
if (!body.content) {
|
||||||
|
connection0.end()
|
||||||
connection1.end()
|
connection1.end()
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
{ success: false, message: 'No valid content provided', data: null },
|
{ success: false, message: 'No valid content provided', data: null },
|
||||||
@@ -47,6 +60,7 @@ export async function handler (context: Context) {
|
|||||||
})
|
})
|
||||||
.execute()
|
.execute()
|
||||||
|
|
||||||
|
connection0.end()
|
||||||
connection1.end()
|
connection1.end()
|
||||||
|
|
||||||
return jsonResponse({ success: true, message: null, data: null }, 200)
|
return jsonResponse({ success: true, message: null, data: null }, 200)
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
import { Context } from 'elysia'
|
import { Context } from 'elysia'
|
||||||
import { getDatabaseConnection, jsonResponse } from '../../../../lib/util'
|
import {
|
||||||
|
getClientIp,
|
||||||
|
getDatabaseConnection,
|
||||||
|
jsonResponse
|
||||||
|
} from '../../../../lib/util'
|
||||||
import { berryDashUserPosts } from '../../../../lib/tables'
|
import { berryDashUserPosts } from '../../../../lib/tables'
|
||||||
import { and, eq } from 'drizzle-orm'
|
import { and, eq } from 'drizzle-orm'
|
||||||
import { checkAuthorization } from '../../../../lib/bd/auth'
|
import { checkAuthorization } from '../../../../lib/bd/auth'
|
||||||
|
|
||||||
type Body = {
|
|
||||||
liked: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function handler (context: Context) {
|
export async function handler (context: Context) {
|
||||||
const dbInfo0 = getDatabaseConnection(0)
|
const dbInfo0 = getDatabaseConnection(0)
|
||||||
const dbInfo1 = getDatabaseConnection(1)
|
const dbInfo1 = getDatabaseConnection(1)
|
||||||
@@ -17,11 +17,19 @@ export async function handler (context: Context) {
|
|||||||
{ success: false, message: 'Failed to connect to database', data: null },
|
{ success: false, message: 'Failed to connect to database', data: null },
|
||||||
500
|
500
|
||||||
)
|
)
|
||||||
|
const { connection: connection0, db: db0 } = dbInfo1
|
||||||
const { connection: connection1, db: db1 } = dbInfo1
|
const { connection: connection1, db: db1 } = dbInfo1
|
||||||
|
|
||||||
|
const ip = getClientIp(context)
|
||||||
const authorizationToken = context.headers.authorization
|
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) {
|
if (!authResult.valid) {
|
||||||
|
connection0.end()
|
||||||
connection1.end()
|
connection1.end()
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
{ success: false, message: 'Unauthorized', data: null },
|
{ success: false, message: 'Unauthorized', data: null },
|
||||||
@@ -33,6 +41,7 @@ export async function handler (context: Context) {
|
|||||||
let idQuery = context.query.id ? parseInt(context.query.id, 10) : 0
|
let idQuery = context.query.id ? parseInt(context.query.id, 10) : 0
|
||||||
let likedQuery = context.query.liked as string
|
let likedQuery = context.query.liked as string
|
||||||
if (!idQuery || idQuery < 1) {
|
if (!idQuery || idQuery < 1) {
|
||||||
|
connection0.end()
|
||||||
connection1.end()
|
connection1.end()
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
{ success: false, message: 'No valid post ID provided', data: null },
|
{ success: false, message: 'No valid post ID provided', data: null },
|
||||||
@@ -43,6 +52,7 @@ export async function handler (context: Context) {
|
|||||||
!likedQuery ||
|
!likedQuery ||
|
||||||
(likedQuery.toLowerCase() != 'true' && likedQuery.toLowerCase() != 'false')
|
(likedQuery.toLowerCase() != 'true' && likedQuery.toLowerCase() != 'false')
|
||||||
) {
|
) {
|
||||||
|
connection0.end()
|
||||||
connection1.end()
|
connection1.end()
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
{
|
{
|
||||||
@@ -65,7 +75,9 @@ export async function handler (context: Context) {
|
|||||||
)
|
)
|
||||||
.limit(1)
|
.limit(1)
|
||||||
.execute()
|
.execute()
|
||||||
if (!votesResult[0])
|
if (!votesResult[0]) {
|
||||||
|
connection0.end()
|
||||||
|
connection1.end()
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
{
|
{
|
||||||
success: true,
|
success: true,
|
||||||
@@ -74,6 +86,7 @@ export async function handler (context: Context) {
|
|||||||
},
|
},
|
||||||
400
|
400
|
||||||
)
|
)
|
||||||
|
}
|
||||||
const votes = JSON.parse(votesResult[0].votes)
|
const votes = JSON.parse(votesResult[0].votes)
|
||||||
if (votes[userId.toString()]) {
|
if (votes[userId.toString()]) {
|
||||||
let likes = 0
|
let likes = 0
|
||||||
@@ -93,6 +106,7 @@ export async function handler (context: Context) {
|
|||||||
)
|
)
|
||||||
.execute()
|
.execute()
|
||||||
|
|
||||||
|
connection0.end()
|
||||||
connection1.end()
|
connection1.end()
|
||||||
|
|
||||||
let likes = 0
|
let likes = 0
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import axios from 'axios'
|
|
||||||
import { Context } from 'elysia'
|
import { Context } from 'elysia'
|
||||||
import {
|
import {
|
||||||
getClientIp,
|
getClientIp,
|
||||||
|
|||||||
Reference in New Issue
Block a user