diff --git a/index.js b/index.js index 0ddcd6b5f3ca24112135cfbbe85f7778ec4ae848..b57f07d4c28dadc404af58c624e2da706c57857c 100644 --- a/index.js +++ b/index.js @@ -249,7 +249,7 @@ io.on('connection', (socket) => { }) }) -async function getKeyFromVal(receiver){ +/* async function getKeyFromVal(receiver){ return new Promise( () => { var array = Array.from(activeUsers); console.log("usersArray: " + array); @@ -264,7 +264,7 @@ async function getKeyFromVal(receiver){ } } }) -} +} */ //Logs message from Socket to Console io.on('connection', (socket) => { @@ -273,21 +273,49 @@ io.on('connection', (socket) => { }); }); -//Send Image File + +/** +* Summary: +* Function for sending image files +* +* +* Description: +* By triggering the 'sendImage' Event, the function emits the given files url to client +* +* @listens socket.on() 'sendImage' event +*/ io.on("connection", (socket) => { socket.on('sendImage', (file, callback) => { io.emit('image', file); }); }); -//Send Video File +/** +* Summary: +* Function for sending video files +* +* +* Description: +* By triggering the 'sendVideo' Event, the function emits the given files url to client +* +* @listens socket.on() 'sendVideo' event +*/ io.on("connection", (socket) => { socket.on('sendVideo', (file, callback) => { io.emit('video', file); }); }); -//Send Audio File +/** +* Summary: +* Function for sending audio files +* +* +* Description: +* By triggering the 'sendAudio' Event, the function emits the given files url to client +* +* @listens socket.on() 'sendAudio' event +*/ io.on("connection", (socket) => { socket.on('sendAudio', (file, callback) => { io.emit('audio', file); diff --git a/routes/auth.js b/routes/auth.js index 2b75e32124b14f26ca5e280c8e524b07c533337e..724a0be7e4b346c6c10a0be8b17467844f210a80 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -8,6 +8,25 @@ const bodyParser = require('body-parser'); const { verify } = require('crypto'); require('dotenv').config(); +/** +* Summary: +* Handles authorization of client. +* +* Description: +* The function receives clients webtoken over the authorization part of the request header. By using JWT as technology, we can +* verify the token of the client. The signature of the client has to be valid, otherwise the function sends 401 as status code. +* JWT provides a assymetric encryption process. +* +* +* @listens fetch '/register' from client +* +* @param {Object} res Description. +* @param {Object} req Description of optional variable. +* +* +* @return {HTTP statusCode} 200 +* @return {HTTP statusCode} 401 +*/ router.get('/', async(req, res) => { authHeader = req.headers.authorization; diff --git a/routes/login.js b/routes/login.js index 85541610369ff2271e8757abe1cccb39676b5eea..f9b39d318025c4984792a116355a9a661936490f 100644 --- a/routes/login.js +++ b/routes/login.js @@ -12,7 +12,7 @@ require('dotenv').config(); /** * Summary: -* Function tto verify the Login of a client. +* Function to verify the Login of a client. * * Description: * Username and password of client get checked, whether they exist in the database. diff --git a/views/index.html b/views/index.html index 6c10c3ecbd010ab21ae1c88577f7a53485ed24a2..807c95b63e42197c852489d1029f480b162be107 100644 --- a/views/index.html +++ b/views/index.html @@ -272,14 +272,18 @@ }); - - - - // Alex schreib nen Kommentar + /** + * Summary: + * Provides url of the given file to share it in the chatroom + * + * Description: + * The functions reads the type of the given file and fires the specific socket.emit event for each file type. + * The function emits the file Object URL over the specific event event + * + */ function upload(files) { var fileObject = files[0]; var url = URL.createObjectURL(fileObject); - console.log(files[0].type.split("/")[0]); var type = files[0].type.split("/")[0]; if(type == "image"){ socket.emit("sendImage", url); @@ -292,9 +296,16 @@ } } - //Receive Multimedia Files from Server + /** + * Summary: + * Receives image files + * + * Description: + * By triggering the image event, the function creates a image object wich takes the file url as source. After setting width and height + * of the image field, the object gets appended to the HTML file + * + */ socket.on('image', function(file) { - console.log(file); var image = new Image(); image.height = 400; image.width = 400; @@ -302,6 +313,15 @@ messages.appendChild(image); }); + /** + * Summary: + * Receives video files + * + * Description: + * By triggering the video event, the function creates a image object wich takes the file url as source. After setting width and height + * of the video field, the object gets appended to the HTML file + * + */ socket.on('video', function(file) { const videoHTML = document.createElement('video'); videoHTML.src = file; @@ -311,6 +331,15 @@ messages.appendChild(videoHTML); }); + /** + * Summary: + * Receives audio files + * + * Description: + * By triggering the audio event, the function creates a image object wich takes the file url as source. After setting the attributes of the html audio + * tag, the element gets appendet to the HTML file + * + */ socket.on('audio', function(file) { const audioHTML = document.createElement('audio'); audioHTML.id = 'audioPlayer'; @@ -320,20 +349,20 @@ messages.appendChild(audioHTML); }); -/** -* Summary: -* Function for getting values from cookie. -* -* Description: -* This function gets the name of a key and returns it's value from a cookie. -* -* -* @ -* -* @param {String} name name of the key, it's value is wished. -* -* @return {String} value of the key in cookie. -*/ + /** + * Summary: + * Function for getting values from cookie. + * + * Description: + * This function gets the name of a key and returns it's value from a cookie. + * + * + * @ + * + * @param {String} name name of the key, it's value is wished. + * + * @return {String} value of the key in cookie. + */ function getCookie(name) { const value = `; ${document.cookie}`; const parts = value.split(`; ${name}=`); diff --git a/views/login.html b/views/login.html index 737712a10513a6ab4bd64ed7d96d0835a5609935..b6dce1699f568d18958384d74fdf08f84dc47bc8 100644 --- a/views/login.html +++ b/views/login.html @@ -22,6 +22,22 @@ const form = document.getElementById('login-form'); form.addEventListener('submit', submitLoginData); + /** + * Summary: + * Handles Login of Client. + * + * Description: + * Logindata of Client, username and password are sent to server. + * The Server responses status code 200 if the client is registred and a valid web token provided by JWT. + * The token gets saved as a cookie. If the status code is 200, the authentication function authToken() gets called. + * + * + * @fires fetch to "/login" + * @fires function getCookie() + * @fires function authToken() + * @listens submit event of "Submit" Button + */ + async function submitLoginData(event) { event.preventDefault(); @@ -59,6 +75,20 @@ .catch(error => console.log('error', error)); } + /** + * Summary: + * Function for getting values from cookie. + * + * Description: + * This function gets the name of a key and returns it's value from a cookie. + * + * + * @ + * + * @param {String} name name of the key, it's value is wished. + * + * @return {String} value of the key in cookie. + */ function getCookie(name) { const value = `; ${document.cookie}`; const parts = value.split(`; ${name}=`); @@ -66,6 +96,19 @@ if (parts.length === 2) return parts.pop().split(';').shift(); } + /** + * Summary: + * Handles the authentication of clients web token + * + * Description: + * The provided web token get set as a header for the http GET request. If the token is valid, the server sends back a status code of 200. + * If the status code is 200, the function redirects in the chatroom + * + * + * @fires fetch to "/authenticate" + * @fires function setCookie() + * @listens for call in submitLoginData() + */ async function authToken(){ var token = getCookie("token").split(','); var myHeaders = new Headers(); @@ -80,23 +123,19 @@ fetch("http://localhost:3000/authenticate", requestOptions) .then((response) => { - // Weiterleitung an Chatroom, bei erfolreicher Anmeldung (StatusCode == 200) + if (response.status === 200) { - // Rückmeldung an Client + var divErfolgreich = document.createElement('div'); divErfolgreich.textContent = "Erfolgreich eingeloggt! Sie werden in den Chatroom weitergeleitet"; document.body.appendChild(divErfolgreich); - // Speichern des Username in Cookie - setCookie(username.value.trim()); - /* socket.emit('userLogin', { - name : username.value.trim() - }); */ - // Kurzer Timeout, dass der Client die Nachricht lesen kann + + setCookie(username.value.trim()); setTimeout(function() { window.location.href = "/chatroom"; }, 2000); } - // Bei nicht erfolgreichem Login (StatusCode != 200) werden die Eingabefelder geleert und dem Client eine Rückmeldung gegeben + else { username.value = ""; password.value = ""; @@ -109,6 +148,13 @@ .catch(error => console.log('error', error)); } + /** + * Summary: + * Sets cookie by given name + * + * + * @param {String} name name of the cookie + */ function setCookie(name) { document.cookie = "username=" + name; } diff --git a/views/start.html b/views/start.html index 44ba2aa73db41458c9f02317dedb00158f3ed119..5d1af8a658159064722424bf8e477ac9362b6724 100644 --- a/views/start.html +++ b/views/start.html @@ -7,7 +7,7 @@ <title>Document</title> </head> <body> - <!-- Start Seite, mit Buttons, die zur Loginseite/ Registrierungsseite weiterleiten--> + <!--Page to redirect to login or registration formular--> <h1>Guten Tag!</h1> <a href="/login">Login</a>