improve backend logic
This commit is contained in:
@ -4,6 +4,7 @@ import { createServer } from "http"
|
||||
import { Server } from "socket.io"
|
||||
|
||||
import { z } from "zod"
|
||||
import { User } from "./types"
|
||||
|
||||
dotenv.config()
|
||||
|
||||
@ -17,31 +18,77 @@ const io = new Server(httpServer, {
|
||||
},
|
||||
})
|
||||
|
||||
const handshakeSchema = z.object({
|
||||
userId: z.string(),
|
||||
sandboxId: z.string(),
|
||||
type: z.enum(["node", "react"]),
|
||||
EIO: z.string(),
|
||||
transport: z.string(),
|
||||
})
|
||||
|
||||
io.use(async (socket, next) => {
|
||||
const q = socket.handshake.query
|
||||
|
||||
console.log("middleware")
|
||||
console.log(q)
|
||||
|
||||
if (!q.userId || !q.sandboxId) {
|
||||
const parseQuery = handshakeSchema.safeParse(q)
|
||||
|
||||
if (!parseQuery.success) {
|
||||
console.log("Invalid request.")
|
||||
next(new Error("Invalid request."))
|
||||
return
|
||||
}
|
||||
|
||||
const dbUser = await fetch(`http://localhost:8787/api/user?id=${q.userId}`)
|
||||
const dbUserJSON = await dbUser.json()
|
||||
const query = parseQuery.data
|
||||
|
||||
if (!dbUserJSON || !dbUserJSON.sandbox.includes(q.sandboxId)) {
|
||||
const dbUser = await fetch(
|
||||
`http://localhost:8787/api/user?id=${query.userId}`
|
||||
)
|
||||
const dbUserJSON = (await dbUser.json()) as User
|
||||
|
||||
console.log("dbUserJSON:", dbUserJSON)
|
||||
|
||||
if (!dbUserJSON) {
|
||||
console.log("DB error.")
|
||||
next(new Error("DB error."))
|
||||
return
|
||||
}
|
||||
|
||||
const sandbox = dbUserJSON.sandbox.find((s) => s.id === query.sandboxId)
|
||||
|
||||
if (!sandbox) {
|
||||
console.log("Invalid credentials.")
|
||||
next(new Error("Invalid credentials."))
|
||||
return
|
||||
}
|
||||
|
||||
const data = {
|
||||
userId: query.userId,
|
||||
sandboxId: query.sandboxId,
|
||||
type: query.type,
|
||||
init: sandbox.init,
|
||||
}
|
||||
|
||||
socket.data = data
|
||||
|
||||
next()
|
||||
})
|
||||
|
||||
io.on("connection", async (socket) => {
|
||||
console.log(`connection`)
|
||||
const userId = socket.handshake.query.userId
|
||||
const data = socket.data as {
|
||||
userId: string
|
||||
sandboxId: string
|
||||
type: "node" | "react"
|
||||
init: boolean
|
||||
}
|
||||
|
||||
console.log(userId)
|
||||
console.log("init:", data.init)
|
||||
|
||||
if (!data.init) {
|
||||
// const dbUser = await fetch(
|
||||
// `http://localhost:8787/sandbox/${data.sandboxId}/init`
|
||||
// )
|
||||
}
|
||||
|
||||
// socket.emit("loaded", {
|
||||
// rootContent: await fetchDir("/workspace", "")
|
||||
|
17
backend/server/src/types.ts
Normal file
17
backend/server/src/types.ts
Normal file
@ -0,0 +1,17 @@
|
||||
// DB Types
|
||||
|
||||
export type User = {
|
||||
id: string
|
||||
name: string
|
||||
email: string
|
||||
sandbox: Sandbox[]
|
||||
}
|
||||
|
||||
export type Sandbox = {
|
||||
id: string
|
||||
name: string
|
||||
type: "react" | "node"
|
||||
init: boolean
|
||||
bucket: string | null
|
||||
userId: string
|
||||
}
|
Reference in New Issue
Block a user