-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
363 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
{ | ||
"cSpell.words": [ | ||
"Fastify", | ||
"Siwe", | ||
"viem" | ||
] | ||
} | ||
"cSpell.words": ["Fastify", "Siwe", "socketio", "viem"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,75 @@ | ||
import fastify, { FastifyInstance, FastifyRequest } from "fastify"; | ||
import { container } from "../../di.js"; | ||
import fastifyPlugin from "fastify-plugin"; | ||
|
||
import { userModel } from "./model.js"; | ||
import { jwtDecode } from "jwt-decode"; | ||
import { set } from "mongoose"; | ||
export const userService = fastifyPlugin( | ||
(fastify: FastifyInstance, opts, done) => { | ||
fastify.get("/me", { | ||
onRequest: [fastify.authenticate], | ||
handler: async (request: FastifyRequest, reply) => { | ||
const userService = container.cradle.userService; | ||
const requestUser = request.user; | ||
const user = await userService.findById(requestUser._id); | ||
if (!user) { | ||
throw new Error(`User not found with id ${requestUser._id}`); | ||
} | ||
return { | ||
walletAddress: user.walletAddress, | ||
_id: user._id.toString(), | ||
nonce: user.nonce.toString(), | ||
currentAllocation: { | ||
id: user.currentAllocationId, | ||
}, | ||
currentActivity: { | ||
id: user.currentActivityId, | ||
}, | ||
deposits: user.deposits.map((d) => { | ||
return { | ||
isCurrent: d.isCurrent, | ||
isValid: d.isValid, | ||
nonce: d.nonce.toString(), | ||
}; | ||
}), | ||
}; | ||
}, | ||
fastify.io.of("/me").use((socket, next) => { | ||
const token = socket.handshake.auth.token; | ||
if (!token) { | ||
next(new Error("Authentication error")); | ||
} | ||
next(); | ||
}); | ||
|
||
fastify.io.of("/me").on("connection", async (socket) => { | ||
console.log("socket", socket.handshake.auth.token); | ||
|
||
const user = jwtDecode<{ | ||
_id: string; | ||
}>(socket.handshake.auth.token); | ||
|
||
if (!user._id) { | ||
throw new Error(`Wrong token`); | ||
} | ||
if (!user) { | ||
throw new Error( | ||
`User not found with id ${socket.handshake.auth.token}` | ||
); | ||
} | ||
|
||
const userDTO = await container.cradle.userService.getUserDTO(user._id); | ||
socket.emit("user", userDTO); | ||
userModel | ||
.watch( | ||
[ | ||
{ | ||
$match: { | ||
_id: user._id, | ||
}, | ||
}, | ||
], | ||
{ | ||
fullDocument: "updateLookup", | ||
} | ||
) | ||
.on("change", async () => { | ||
const userDTO = await container.cradle.userService.getUserDTO( | ||
user._id | ||
); | ||
socket.emit("user", userDTO); | ||
}); | ||
}); | ||
// fastify.get( | ||
// "/me", | ||
// { | ||
// websocket: true, | ||
// onRequest: [fastify.authenticate], | ||
// }, | ||
// async (socket, request) => { | ||
// console.log("socket", request.user._id); | ||
// const userService = container.cradle.userService; | ||
// const requestUser = request.user; | ||
// const user = await userService.findById(requestUser._id); | ||
// console.log("user", user); | ||
// if (!user) { | ||
// throw new Error(`User not found with id ${requestUser._id}`); | ||
// } | ||
|
||
// } | ||
// ); | ||
done(); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { FastifyInstance, FastifyPluginAsync } from "fastify"; | ||
import fp from "fastify-plugin"; | ||
import { Server, ServerOptions } from "socket.io"; | ||
import { IUser } from "./services/user/types.js"; | ||
|
||
const fastifySocketIO: FastifyPluginAsync<Partial<ServerOptions>> = fp( | ||
async function (fastify, opts) { | ||
const server = new Server< | ||
{}, | ||
{ | ||
user: () => void; | ||
} | ||
>({ | ||
cors: { | ||
origin: "*", | ||
}, | ||
}); | ||
|
||
server.listen(Number(process.env.PORT) || 5174); | ||
|
||
fastify.decorate("io", server); | ||
|
||
fastify.addHook("onClose", (fastify: FastifyInstance, done) => { | ||
(fastify as any).io.close(); | ||
done(); | ||
}); | ||
}, | ||
{ fastify: ">=4.x.x", name: "fastify-socket.io" } | ||
); | ||
|
||
declare module "fastify" { | ||
interface FastifyInstance { | ||
io: Server< | ||
{}, | ||
{ | ||
user: (data: IUser) => void; | ||
} | ||
>; | ||
} | ||
} | ||
|
||
export default fastifySocketIO; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
VITE_BACKEND_URL=http://127.0.0.1:5174/api | ||
VITE_BACKEND_HTTP_URL=http://127.0.0.1:5174/api | ||
VITE_BACKEND_WS_URL=127.0.0.1:5174 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
VITE_BACKEND_URL=http://0.0.0.0:5174/api | ||
VITE_BACKEND_HTTP_URL=https://0.0.0.0:5174/api | ||
VITE_BACKEND_WS_URL=wss://0.0.0.0:5174/api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.