diff --git a/app/controllers/auth.controller.js b/app/controllers/auth.controller.js index 26b81f14d74ac2c059794b21efb22fb37dc4c31b..0f55a6aa33a7dd1d7f495ecfe7467154821dbd69 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 e32a7b1236e440b3eb6b34d0d34c444ea7803cf1..58b0cc38a3eec25ad1aa664542c0e26e25e6c02b 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 c7606216698d1a93ec4f69bab016a09ad706586b..1d3255af4cb8d73d4fab4419d751dae5ecdd0e90 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 9180876bcd2b7dbe3072fc6aae00d1ea712eef76..7937e4771d628c4aea6418b806b20c3bf7d6387c 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 5483ec1d79066fb4dc74681a10f2600e14892cd1..95d587605b9a65083c55cdb86728b9347f547a75 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 42e3904c467c2680bd1dff9744996c3da8646aea..04da754231a8625057c0845a854068b2a56940a6 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);