From be6d755021587458fc7280ccf328dc27636325c1 Mon Sep 17 00:00:00 2001 From: luzzi <christopher.luzzi@student.reutlingen-university.de> Date: Tue, 1 Nov 2022 22:12:06 +0100 Subject: [PATCH] comments added --- app/controllers/auth.controller.js | 10 ++++------ app/middlewares/authJWT.js | 2 ++ app/middlewares/verifySignUp.js | 2 ++ app/routes/auth.routes.js | 1 - crypto.js | 2 ++ server.js | 15 +++++++++++++-- 6 files changed, 23 insertions(+), 9 deletions(-) diff --git a/app/controllers/auth.controller.js b/app/controllers/auth.controller.js index 26b81f1..0f55a6a 100644 --- a/app/controllers/auth.controller.js +++ b/app/controllers/auth.controller.js @@ -5,6 +5,7 @@ const User = db.user; var jwt = require("jsonwebtoken"); var bcrypt = require("bcryptjs"); +//Here we signup an user. The Password provided during signup process gets encrypted and saved into the database exports.signup = (req, res) => { console.log(req.body) const user = new User({ @@ -23,6 +24,8 @@ exports.signup = (req, res) => { }); }; +//Signing in we compare the hased passwort from the database with the password provided while signing in. +//If it matches we sign an token that expires after 24 hours exports.signin = (req, res) => { User.findOne({ username: req.body.username, @@ -54,15 +57,10 @@ exports.signin = (req, res) => { req.session.token = token; res.redirect('/chat') - - // res.status(200).send({ - // id: user._id, - // username: user.username, - // email: user.email - // }); }); }; +//Signing out exports.signout = async (req, res) => { try { req.session = null; diff --git a/app/middlewares/authJWT.js b/app/middlewares/authJWT.js index e32a7b1..58b0cc3 100644 --- a/app/middlewares/authJWT.js +++ b/app/middlewares/authJWT.js @@ -3,6 +3,8 @@ const config = require("../config/auth.config.js"); const db = require("../models"); const User = db.user; + +//Here we check if the token created at the signup is correct. We need this to access whenever we request data we need to be logged in verifyToken = (req, res, next) => { let token = req.session.token; diff --git a/app/middlewares/verifySignUp.js b/app/middlewares/verifySignUp.js index c760621..1d3255a 100644 --- a/app/middlewares/verifySignUp.js +++ b/app/middlewares/verifySignUp.js @@ -1,6 +1,8 @@ const db = require("../models"); const User = db.user; +//Checks for Duplicated Usernames or emails during registration +//At our Database we lookup for existing entries with the username or email. If one exists we return the fitting error checkDuplicateUsernameOrEmail = (req, res, next) => { // Username console.log(req.body) diff --git a/app/routes/auth.routes.js b/app/routes/auth.routes.js index 9180876..7937e47 100644 --- a/app/routes/auth.routes.js +++ b/app/routes/auth.routes.js @@ -14,7 +14,6 @@ module.exports = function(app) { app.post( "/api/auth/signup", - // verifySignUp.checkDuplicateUsernameOrEmail, controller.signup ); diff --git a/crypto.js b/crypto.js index 5483ec1..95d5876 100644 --- a/crypto.js +++ b/crypto.js @@ -1,3 +1,5 @@ +//this is to define the encryption and decryption functions +//i got this script from this tutorial: https://attacomsian.com/blog/nodejs-encrypt-decrypt-data const crypto = require('crypto') const algorithm = 'aes-256-ctr' diff --git a/server.js b/server.js index 42e3904..04da754 100644 --- a/server.js +++ b/server.js @@ -1,3 +1,4 @@ +//Inports const express = require('express') const cors = require('cors') const cookieSession = require('cookie-session') @@ -11,11 +12,12 @@ const io = new Server(server); const { encrypt, decrypt } = require('./crypto') - +//Setting up Cross Origin var corsOption = { origin: "http://localhost:3001" } +//Setting up Middle to use app.use(cors(corsOption)) app.use(express.json()) @@ -30,13 +32,19 @@ app.use( }) ); + app.set(path.join(__dirname, '/app')) +//setting the view engine to html app.engine('html', ejs.renderFile) app.set('view engine', 'html') +//setting the path app.use(express.static(path.join(__dirname, "/app"))); + +//importing the user model const db = require("./app/models"); +//connceting to the Database db.mongoose .connect(`mongodb+srv://cici:cloudcomputing@cloud-computing.cl9pyt6.mongodb.net/?retryWrites=true&w=majority`, { useNewUrlParser: true, @@ -51,6 +59,7 @@ db.mongoose }); +//setting up the endpoints app.get('/start', (req,res) =>{ res.render('register.html') }) @@ -63,15 +72,17 @@ app.get('/index', (req, res) => { res.sendFile(__dirname + '/index.html'); }); +//connecting to the socket io.on('connection', (socket) => { socket.on('chat message', (msg) => { + //Here we encrypt the msg before sending it to the client io.emit('chat message', encrypt(msg).content); }); }); - +//Importing routes require('./app/routes/auth.routes')(app); require('./app/routes/user.routes')(app); -- GitLab