Skip to content
Snippets Groups Projects
Commit be6d7550 authored by Christopher Luzzi's avatar Christopher Luzzi
Browse files

comments added

parent ab9601c7
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......
......@@ -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;
......
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)
......
......@@ -14,7 +14,6 @@ module.exports = function(app) {
app.post(
"/api/auth/signup",
// verifySignUp.checkDuplicateUsernameOrEmail,
controller.signup
);
......
//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'
......
//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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment