diff options
| author | Adam Soltys <[email protected]> | 2023-01-13 20:27:18 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-01-13 20:27:18 -0800 |
| commit | 8a114c50efaac41767042766f05390aa906da157 (patch) | |
| tree | eb65bc1e9adf3614cb30798210328f7e83e0a0ce /lib/sockets.js | |
| parent | c3565838644123e77881ff4f8536792f074047b9 (diff) | |
| download | coinos-server-8a114c50efaac41767042766f05390aa906da157.tar.gz coinos-server-8a114c50efaac41767042766f05390aa906da157.zip | |
migrating to redis
Diffstat (limited to 'lib/sockets.js')
| -rw-r--r-- | lib/sockets.js | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/lib/sockets.js b/lib/sockets.js index 0f343937..c94c29a0 100644 --- a/lib/sockets.js +++ b/lib/sockets.js @@ -8,7 +8,7 @@ import { differenceInSeconds, subSeconds } from "date-fns"; import fastifyWs from "@fastify/websocket"; import whitelist from "./whitelist"; import { l, err, warn } from "$lib/logging"; -import { getUser } from "$lib/utils"; +import { g } from "$lib/redis"; const code = 1000; @@ -92,24 +92,24 @@ app.register(async function(app) { if (!token || token === "null" || !jwt.decode(token)) return ws.close(code, `bad token ${token}`); - let { uuid } = jwt.decode(token); - if (!uuid) throw new Error("Invalid JWT token"); - console.log("UUID", uuid) - let user = await getUser(uuid); + let { id: user_id } = jwt.decode(token); + if (!user_id) throw new Error("Invalid JWT token"); + let user = await g(`user:${user_id}`); if (!user) throw new Error("User not found"); + user.haspin = !!user.pin; - if (!store.sockets[uuid]) store.sockets[uuid] = {}; - const existing = Object.keys(store.sockets[uuid]); + if (!store.sockets[user_id]) store.sockets[user_id] = {}; + const existing = Object.keys(store.sockets[user_id]); if (existing.length > 4) { - const p = existing.find(sid => sid !== id); - store.sockets[uuid][p].send(JSON.stringify({ type: "logout" })); + const p = existing.find(sid => sid !== user_id); + store.sockets[user_id][p].send(JSON.stringify({ type: "logout" })); setTimeout(() => { - if (store.sockets[uuid][p]) - store.sockets[uuid][p].close(code, "too many sockets"); + if (store.sockets[user_id][p]) + store.sockets[user_id][p].close(code, "too many sockets"); }, 5000); } - store.sockets[uuid][id] = ws; - names[id] = uuid; + store.sockets[user_id][id] = ws; + names[id] = user_id; ws.user = user; ws.send( |
