Make a auth lib for Berry Dash
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { Context } from 'elysia'
|
||||
import { getDatabaseConnection, jsonResponse } from '../../../../lib/util'
|
||||
import { berryDashUserData, berryDashUserPosts } from '../../../../lib/tables'
|
||||
import { berryDashUserPosts } from '../../../../lib/tables'
|
||||
import { and, eq } from 'drizzle-orm'
|
||||
import { checkAuthorization } from '../../../../lib/bd/auth'
|
||||
|
||||
export async function handler (context: Context) {
|
||||
const dbInfo0 = getDatabaseConnection(0)
|
||||
@@ -14,7 +15,17 @@ export async function handler (context: Context) {
|
||||
)
|
||||
const { connection: connection1, db: db1 } = dbInfo1
|
||||
|
||||
let authorizationToken = context.headers.authorization
|
||||
const authorizationToken = context.headers.authorizationToken
|
||||
const authResult = await checkAuthorization(authorizationToken as string, db1)
|
||||
if (!authResult.valid) {
|
||||
connection1.end()
|
||||
return jsonResponse(
|
||||
{ success: false, message: 'Unauthorized', data: null },
|
||||
401
|
||||
)
|
||||
}
|
||||
const userId = authResult.id
|
||||
|
||||
let idQuery = context.query.id ? parseInt(context.query.id, 10) : 0
|
||||
if (!idQuery || idQuery < 1) {
|
||||
connection1.end()
|
||||
@@ -23,27 +34,6 @@ export async function handler (context: Context) {
|
||||
400
|
||||
)
|
||||
}
|
||||
if (!authorizationToken) {
|
||||
connection1.end()
|
||||
return jsonResponse(
|
||||
{ success: false, message: 'Unauthorized', data: null },
|
||||
401
|
||||
)
|
||||
}
|
||||
|
||||
const userData = await db1
|
||||
.select({ id: berryDashUserData.id })
|
||||
.from(berryDashUserData)
|
||||
.where(eq(berryDashUserData.token, authorizationToken))
|
||||
.execute()
|
||||
|
||||
if (!userData[0]) {
|
||||
connection1.end()
|
||||
return jsonResponse(
|
||||
{ success: false, message: 'Unauthorized', data: null },
|
||||
401
|
||||
)
|
||||
}
|
||||
|
||||
const result = await db1
|
||||
.update(berryDashUserPosts)
|
||||
@@ -51,7 +41,7 @@ export async function handler (context: Context) {
|
||||
.where(
|
||||
and(
|
||||
eq(berryDashUserPosts.id, idQuery),
|
||||
eq(berryDashUserPosts.userId, userData[0].id),
|
||||
eq(berryDashUserPosts.userId, userId),
|
||||
eq(berryDashUserPosts.deletedAt, 0)
|
||||
)
|
||||
)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Context } from 'elysia'
|
||||
import { getDatabaseConnection, jsonResponse } from '../../../../lib/util'
|
||||
import { berryDashUserData, berryDashUserPosts } from '../../../../lib/tables'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { berryDashUserPosts } from '../../../../lib/tables'
|
||||
import { checkAuthorization } from '../../../../lib/bd/auth'
|
||||
|
||||
type Body = {
|
||||
content: string
|
||||
@@ -18,7 +18,17 @@ export async function handler (context: Context) {
|
||||
)
|
||||
const { connection: connection1, db: db1 } = dbInfo1
|
||||
|
||||
let authorizationToken = context.headers.authorization
|
||||
const authorizationToken = context.headers.authorizationToken
|
||||
const authResult = await checkAuthorization(authorizationToken as string, db1)
|
||||
if (!authResult.valid) {
|
||||
connection1.end()
|
||||
return jsonResponse(
|
||||
{ success: false, message: 'Unauthorized', data: null },
|
||||
401
|
||||
)
|
||||
}
|
||||
const userId = authResult.id
|
||||
|
||||
const body = context.body as Body
|
||||
if (!body.content) {
|
||||
connection1.end()
|
||||
@@ -27,32 +37,11 @@ export async function handler (context: Context) {
|
||||
400
|
||||
)
|
||||
}
|
||||
if (!authorizationToken) {
|
||||
connection1.end()
|
||||
return jsonResponse(
|
||||
{ success: false, message: 'Unauthorized', data: null },
|
||||
401
|
||||
)
|
||||
}
|
||||
|
||||
const userData = await db1
|
||||
.select({ id: berryDashUserData.id })
|
||||
.from(berryDashUserData)
|
||||
.where(eq(berryDashUserData.token, authorizationToken))
|
||||
.execute()
|
||||
|
||||
if (!userData[0]) {
|
||||
connection1.end()
|
||||
return jsonResponse(
|
||||
{ success: false, message: 'Unauthorized', data: null },
|
||||
401
|
||||
)
|
||||
}
|
||||
|
||||
await db1
|
||||
.insert(berryDashUserPosts)
|
||||
.values({
|
||||
userId: userData[0].id,
|
||||
userId: userId,
|
||||
content: btoa(body.content),
|
||||
timestamp: Math.floor(Date.now() / 1000)
|
||||
})
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { Context } from 'elysia'
|
||||
import { getDatabaseConnection, jsonResponse } from '../../../../lib/util'
|
||||
import { berryDashUserData, berryDashUserPosts } from '../../../../lib/tables'
|
||||
import { berryDashUserPosts } from '../../../../lib/tables'
|
||||
import { and, eq } from 'drizzle-orm'
|
||||
import { checkAuthorization } from '../../../../lib/bd/auth'
|
||||
|
||||
type Body = {
|
||||
liked: string
|
||||
@@ -18,7 +19,17 @@ export async function handler (context: Context) {
|
||||
)
|
||||
const { connection: connection1, db: db1 } = dbInfo1
|
||||
|
||||
let authorizationToken = context.headers.authorization
|
||||
const authorizationToken = context.headers.authorizationToken
|
||||
const authResult = await checkAuthorization(authorizationToken as string, db1)
|
||||
if (!authResult.valid) {
|
||||
connection1.end()
|
||||
return jsonResponse(
|
||||
{ success: false, message: 'Unauthorized', data: null },
|
||||
401
|
||||
)
|
||||
}
|
||||
const userId = authResult.id
|
||||
|
||||
let idQuery = context.query.id ? parseInt(context.query.id, 10) : 0
|
||||
let likedQuery = context.query.liked as string
|
||||
if (!idQuery || idQuery < 1) {
|
||||
@@ -42,27 +53,6 @@ export async function handler (context: Context) {
|
||||
400
|
||||
)
|
||||
}
|
||||
if (!authorizationToken) {
|
||||
connection1.end()
|
||||
return jsonResponse(
|
||||
{ success: false, message: 'Unauthorized', data: null },
|
||||
401
|
||||
)
|
||||
}
|
||||
|
||||
const userData = await db1
|
||||
.select({ id: berryDashUserData.id })
|
||||
.from(berryDashUserData)
|
||||
.where(eq(berryDashUserData.token, authorizationToken))
|
||||
.execute()
|
||||
|
||||
if (!userData[0]) {
|
||||
connection1.end()
|
||||
return jsonResponse(
|
||||
{ success: false, message: 'Unauthorized', data: null },
|
||||
401
|
||||
)
|
||||
}
|
||||
|
||||
const votesResult = await db1
|
||||
.select({ votes: berryDashUserPosts.votes })
|
||||
@@ -85,12 +75,12 @@ export async function handler (context: Context) {
|
||||
400
|
||||
)
|
||||
const votes = JSON.parse(votesResult[0].votes)
|
||||
if (votes[userData[0].id.toString()]) {
|
||||
if (votes[userId.toString()]) {
|
||||
let likes = 0
|
||||
for (const vote of Object.values(votes) as boolean[]) likes += vote ? 1 : -1
|
||||
return jsonResponse({ success: true, message: null, data: { likes } }, 200)
|
||||
}
|
||||
votes[userData[0].id.toString()] = likedQuery.toLowerCase() == 'true'
|
||||
votes[userId.toString()] = likedQuery.toLowerCase() == 'true'
|
||||
|
||||
await db1
|
||||
.update(berryDashUserPosts)
|
||||
|
||||
Reference in New Issue
Block a user