diff --git a/src/pages/debug/utils.js b/src/pages/debug/utils.js index d96fcc8..dbb92d8 100644 --- a/src/pages/debug/utils.js +++ b/src/pages/debug/utils.js @@ -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( diff --git a/src/pages/index.js b/src/pages/index.js index 5a404b3..f09dade 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -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() +}; diff --git a/src/utils/auth.js b/src/utils/auth.js index 76165af..3dfa666 100644 --- a/src/utils/auth.js +++ b/src/utils/auth.js @@ -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; } diff --git a/src/utils/streams.js b/src/utils/streams.js index 014b042..8b01319 100644 --- a/src/utils/streams.js +++ b/src/utils/streams.js @@ -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} + */ +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 };