|
|
|
@ -9,6 +9,8 @@ import { ChangeSet, Text } from "@codemirror/state";
|
|
|
|
|
import { Update, rebaseUpdates } from "@codemirror/collab";
|
|
|
|
|
import * as db from "./database";
|
|
|
|
|
import bcrypt from "bcrypt";
|
|
|
|
|
import { fastifySession } from "@fastify/session";
|
|
|
|
|
import { fastifyCookie } from "@fastify/cookie";
|
|
|
|
|
|
|
|
|
|
const sender = new Push();
|
|
|
|
|
await sender.bind(`tcp://127.0.0.1:5557`);
|
|
|
|
@ -35,6 +37,8 @@ const fastify = Fastify({
|
|
|
|
|
}).withTypeProvider<TypeBoxTypeProvider>();
|
|
|
|
|
await fastify.register(cors, {
|
|
|
|
|
origin: process.env.ALLOW_ORIGIN || "*",
|
|
|
|
|
credentials: true,
|
|
|
|
|
methods: ["GET", "POST", "PUT", "DELETE"],
|
|
|
|
|
});
|
|
|
|
|
fastify.register(websocket);
|
|
|
|
|
fastify.get("/live", { websocket: true }, (socket, req) => {
|
|
|
|
@ -70,6 +74,25 @@ fastify.get("/live", { websocket: true }, (socket, req) => {
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Création de la route pour la gestion des cookies pour la session
|
|
|
|
|
fastify.register(fastifyCookie);
|
|
|
|
|
fastify.register(fastifySession, {
|
|
|
|
|
secret: "8jYuS75JZuxb6C72nDtH2cY6hnV4B7i35r5c39gQ3h9G9DApAweBsQ47dU9DGpk5",
|
|
|
|
|
cookie: {
|
|
|
|
|
secure: true,
|
|
|
|
|
sameSite: "none",
|
|
|
|
|
partitioned: true,
|
|
|
|
|
},
|
|
|
|
|
saveUninitialized: false,
|
|
|
|
|
cookieName: "my-session-cookie",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
declare module "fastify" {
|
|
|
|
|
interface Session {
|
|
|
|
|
userKey: string | null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fastify.post(
|
|
|
|
|
"/run",
|
|
|
|
|
{
|
|
|
|
@ -83,6 +106,7 @@ fastify.post(
|
|
|
|
|
(req, reply) => {
|
|
|
|
|
const { code, language } = req.body;
|
|
|
|
|
const runner = getRunner(language);
|
|
|
|
|
|
|
|
|
|
if (runner === null) {
|
|
|
|
|
return reply.status(422).send({ error: "Invalid language" });
|
|
|
|
|
}
|
|
|
|
@ -160,11 +184,20 @@ fastify.post(
|
|
|
|
|
if (user === null || !(await bcrypt.compare(password, user.password))) {
|
|
|
|
|
reply.send({ success: false });
|
|
|
|
|
} else {
|
|
|
|
|
request.session.userKey = generateId();
|
|
|
|
|
console.log(request.session.userKey);
|
|
|
|
|
reply.send({ success: true });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/* Route pour se déconnecter */
|
|
|
|
|
fastify.post("/users/logout", async (request, reply) => {
|
|
|
|
|
console.log(request.session.userKey);
|
|
|
|
|
request.session.destroy();
|
|
|
|
|
reply.send({ success: true });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/* Route pour mettre à jour le login d'un utilisateur */
|
|
|
|
|
fastify.put(
|
|
|
|
|
"/users/:id/login",
|
|
|
|
@ -282,6 +315,10 @@ fastify.delete("/users", async (request, reply) => {
|
|
|
|
|
|
|
|
|
|
/* Route pour récupérer tous les utilisateurs */
|
|
|
|
|
fastify.get("/users", async (request, reply) => {
|
|
|
|
|
console.log(request.session.userKey);
|
|
|
|
|
|
|
|
|
|
console.log(request.session.userKey);
|
|
|
|
|
|
|
|
|
|
const users = await db.selectAllUsers(database);
|
|
|
|
|
reply.send(users);
|
|
|
|
|
});
|
|
|
|
@ -300,9 +337,12 @@ fastify.get(
|
|
|
|
|
},
|
|
|
|
|
async (request, reply) => {
|
|
|
|
|
const { id } = request.params;
|
|
|
|
|
console.log(request.session.userKey);
|
|
|
|
|
if (request.session.userKey) {
|
|
|
|
|
const user = await db.selectUserById(database, id);
|
|
|
|
|
reply.send(user);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/* Route pour récupérer un utilisateur par son login */
|
|
|
|
|