hhhhhnnn mimimimimimimimi

This commit is contained in:
Guillem George 2026-05-15 22:33:21 +02:00
parent 0114214995
commit 441f00507d
4 changed files with 68 additions and 9 deletions

View file

@ -1,7 +1,7 @@
import $ from "jquery";
import jwt_decode from "jwt-decode";
import { createAlert } from "../../utils/notify";
import {authedAPIRequest} from "../../utils/auth"
import { authedAPIRequest } from "../../utils/auth";
export async function displayStudentProfile() {
const token = localStorage.getItem("token");
@ -10,7 +10,9 @@ export async function displayStudentProfile() {
const _uid = decoded.uid;
// You have to write a request to fetch your informations
const request_result = await authedAPIRequest(`/students/${_uid}`, {method: "GET"});
const request_result = await authedAPIRequest(`/students/${_uid}`, {
method: "GET",
});
if (request_result === null) {
createAlert(

View file

@ -1,6 +1,16 @@
// FIXME: This is the entry point of the application, write your code here
import { initSocket } from "../utils/streams";
import { calculateLayout } from "./utils";
import { authenticate } from "../utils/auth";
// Initialize the layout
calculateLayout();
async () => {
// ? Not sure
if (!(await authenticate())) {
return;
}
initSocket();
// subscribe()
};

View file

@ -114,7 +114,7 @@ async function authenticate() {
const refresh_token = localStorage.getItem("refresh_token");
if (refresh_token !== null) {
return true;
return await refreshToken(refresh_token);
}
localStorage.clear();
@ -130,13 +130,13 @@ async function authenticate() {
* body, rather than just the body
*/
async function authedAPIRequest(endpoint, options) {
if (!authenticate()) {
if (!(await authenticate())) {
return null;
}
if (!options.method) {
console.error("Invalid parameter: options (missing method)");
return null
return null;
}
if (!options.headers) {
@ -159,12 +159,12 @@ async function authedAPIRequest(endpoint, options) {
const response_err = await response.text();
if (response_err.includes("Token expired")) {
refreshToken(null);
await refreshToken(null);
return null;
}
localStorage.clear();
alert("Redirecting to logging page")
alert("Redirecting to logging page");
redirect.redirectToLoginPage();
return null;
}

View file

@ -1,3 +1,6 @@
import { io } from "socket.io-client";
// import { v4 as uuidv4 } from "uuid";
// FIXME: This file should handle the sockets and the subscriptions
// Exports must include
// - initSocket (initialize the connection to the socket server)
@ -7,3 +10,47 @@
// - subscribe (subscribe to a room's stream or chat)
// - unsubscribe (unsubscribe from a room's stream or chat)
// - sendMessage (send a message to a room's chat)
let socket = null;
// let uuid = uuidv4();
/**
* Initializes the socket when authenticated
* returns {Promise<void>}
*/
async function initSocket() {
console.debug("Initializing socket connection");
const token = localStorage.getItem("token");
const host = import.meta.env.VITE_HOST;
const port = import.meta.env.VITE_PORT;
const wsUri = "ws://" + host + (port ? ":" + port : "");
socket = io(wsUri, {
reconnectionDelayMax: 10000,
extraHeaders: {
Authorization: "Bearer " + token,
},
});
console.debug("Socket active: " + socket.active);
return;
}
// TODO
// async function subscribe(room) {
// if (!room)
// room = "epi-place"
// console.warn("Skipping room susbscription (not implemented)")
// }
// async function unsubscribe() {
// }
// async function sendMessage() {
// }
export { initSocket, socket };