Add post/get for account save data
This commit is contained in:
31
src/index.ts
31
src/index.ts
@@ -21,6 +21,9 @@ import { handler as berrydashProfilePostsPutHandler } from './routes/berrydash/p
|
|||||||
|
|
||||||
import { handler as berryDashIconMarketplacePostHandler } from './routes/berrydash/icon-marketplace/post'
|
import { handler as berryDashIconMarketplacePostHandler } from './routes/berrydash/icon-marketplace/post'
|
||||||
|
|
||||||
|
import { handler as berryDashAccountSaveGetHandler } from './routes/berrydash/account/save/get'
|
||||||
|
import { handler as berryDashAccountSavePostHandler } from './routes/berrydash/account/save/post'
|
||||||
|
|
||||||
dotenv.config()
|
dotenv.config()
|
||||||
|
|
||||||
const intNotStr = (name: string) => {
|
const intNotStr = (name: string) => {
|
||||||
@@ -246,6 +249,34 @@ app.post(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
app.get(
|
||||||
|
'/berrydash/account/save',
|
||||||
|
context => berryDashAccountSaveGetHandler(context),
|
||||||
|
{
|
||||||
|
detail: {
|
||||||
|
description:
|
||||||
|
"The endpoint for getting the account's save file. The contents will fully replace the current save file entirely on the client.",
|
||||||
|
tags: ['Berry Dash', 'Accounts']
|
||||||
|
},
|
||||||
|
headers: t.Object({
|
||||||
|
authorization: t.String()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
|
app.post(
|
||||||
|
'/berrydash/account/save',
|
||||||
|
context => berryDashAccountSavePostHandler(context),
|
||||||
|
{
|
||||||
|
detail: {
|
||||||
|
description:
|
||||||
|
"The endpoint for overwriting the account's save file on the server.",
|
||||||
|
tags: ['Berry Dash', 'Accounts']
|
||||||
|
},
|
||||||
|
headers: t.Object({
|
||||||
|
authorization: t.String()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
app.all('*', () =>
|
app.all('*', () =>
|
||||||
jsonResponse(
|
jsonResponse(
|
||||||
{
|
{
|
||||||
|
|||||||
59
src/routes/berrydash/account/save/get.ts
Normal file
59
src/routes/berrydash/account/save/get.ts
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
import { Context } from 'elysia'
|
||||||
|
import { getDatabaseConnection, jsonResponse } from '../../../../lib/util'
|
||||||
|
import { checkAuthorization } from '../../../../lib/bd/auth'
|
||||||
|
import { berryDashUserData, users } from '../../../../lib/tables'
|
||||||
|
import { eq } from 'drizzle-orm'
|
||||||
|
|
||||||
|
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', data: null },
|
||||||
|
500
|
||||||
|
)
|
||||||
|
const { connection: connection0, db: db0 } = dbInfo0
|
||||||
|
const { connection: connection1, db: db1 } = dbInfo1
|
||||||
|
|
||||||
|
const authorizationToken = context.headers.authorization
|
||||||
|
const authResult = await checkAuthorization(authorizationToken as string, db1)
|
||||||
|
if (!authResult.valid) {
|
||||||
|
connection0.end()
|
||||||
|
connection1.end()
|
||||||
|
return jsonResponse(
|
||||||
|
{ success: false, message: 'Unauthorized', data: null },
|
||||||
|
401
|
||||||
|
)
|
||||||
|
}
|
||||||
|
const userId = authResult.id
|
||||||
|
|
||||||
|
const result = await db1
|
||||||
|
.select({
|
||||||
|
saveData: berryDashUserData.saveData,
|
||||||
|
token: berryDashUserData.token
|
||||||
|
})
|
||||||
|
.from(berryDashUserData)
|
||||||
|
.where(eq(berryDashUserData.id, userId))
|
||||||
|
.execute()
|
||||||
|
const result2 = await db0
|
||||||
|
.select({ username: users.username })
|
||||||
|
.from(users)
|
||||||
|
.where(eq(users.id, userId))
|
||||||
|
.execute()
|
||||||
|
|
||||||
|
connection0.end()
|
||||||
|
connection1.end()
|
||||||
|
|
||||||
|
if (!result || !result)
|
||||||
|
return jsonResponse(
|
||||||
|
{ success: false, message: 'Unauthorized', data: null },
|
||||||
|
401
|
||||||
|
)
|
||||||
|
|
||||||
|
let savedata = JSON.parse(result[0].saveData)
|
||||||
|
savedata.account.id = userId
|
||||||
|
savedata.account.name = result2[0].username
|
||||||
|
savedata.account.session = result[0].token
|
||||||
|
return jsonResponse({ success: true, message: null, data: savedata }, 200)
|
||||||
|
}
|
||||||
62
src/routes/berrydash/account/save/post.ts
Normal file
62
src/routes/berrydash/account/save/post.ts
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
import { Context } from 'elysia'
|
||||||
|
import { getDatabaseConnection, jsonResponse } from '../../../../lib/util'
|
||||||
|
import { checkAuthorization } from '../../../../lib/bd/auth'
|
||||||
|
import { berryDashUserData, users } from '../../../../lib/tables'
|
||||||
|
import { eq } from 'drizzle-orm'
|
||||||
|
|
||||||
|
type Body = {
|
||||||
|
saveData: 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 authorizationToken = context.headers.authorization
|
||||||
|
const authResult = await checkAuthorization(authorizationToken as string, db1)
|
||||||
|
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.saveData) {
|
||||||
|
connection0.end()
|
||||||
|
connection1.end()
|
||||||
|
return jsonResponse(
|
||||||
|
{ success: false, message: 'No valid save data provided' },
|
||||||
|
400
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
let userSaveData: any = {}
|
||||||
|
try {
|
||||||
|
userSaveData = JSON.parse(atob(body.saveData))
|
||||||
|
userSaveData.account.id = null
|
||||||
|
userSaveData.account.name = null
|
||||||
|
userSaveData.account.session = null
|
||||||
|
} catch {
|
||||||
|
return jsonResponse(
|
||||||
|
{ success: false, message: "Couldn't parse save data" },
|
||||||
|
400
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
await db1
|
||||||
|
.update(berryDashUserData)
|
||||||
|
.set({ saveData: JSON.stringify(userSaveData) })
|
||||||
|
.where(eq(berryDashUserData.id, userId))
|
||||||
|
.execute()
|
||||||
|
|
||||||
|
return jsonResponse({ success: true, message: null })
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user