|
|
|
@ -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`);
|
|
|
|
@ -40,7 +42,27 @@ const fastify = Fastify({
|
|
|
|
|
type Fastify = typeof fastify;
|
|
|
|
|
await fastify.register(cors, {
|
|
|
|
|
origin: process.env.ALLOW_ORIGIN || "*",
|
|
|
|
|
credentials: true,
|
|
|
|
|
methods: ["GET", "POST", "PUT", "DELETE"],
|
|
|
|
|
});
|
|
|
|
|
fastify.register(fastifyCookie);
|
|
|
|
|
fastify.register(fastifySession, {
|
|
|
|
|
secret: "8jYuS75JZuxb6C72nDtH2cY6hnV4B7i35r5c39gQ3h9G9DApAweBsQ47dU9DGpk5",
|
|
|
|
|
cookie: {
|
|
|
|
|
secure: false,
|
|
|
|
|
sameSite: "none",
|
|
|
|
|
partitioned: true,
|
|
|
|
|
},
|
|
|
|
|
saveUninitialized: false,
|
|
|
|
|
cookieName: "session-id",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
declare module "fastify" {
|
|
|
|
|
interface Session {
|
|
|
|
|
userKey: string | null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fastify.register(websocket);
|
|
|
|
|
fastify.register(async function(fastify: Fastify) {
|
|
|
|
|
fastify.get(
|
|
|
|
@ -211,6 +233,8 @@ 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 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -220,6 +244,13 @@ fastify.post(
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/* 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",
|
|
|
|
@ -337,6 +368,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);
|
|
|
|
|
});
|
|
|
|
@ -355,9 +390,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 */
|
|
|
|
|