-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth.js
127 lines (109 loc) · 3.46 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
require("dotenv").config();
var express = require("express");
const router = express.Router();
const User = require("../models/User.js");
const nodemailer = require("nodemailer");
const Logger = require("../logger.js");
const Session = require("../models/Session.js");
router.post("/login", (req, res) => {
let responseBody = {
found: false,
room: null,
code: null,
};
console.log(req.body.code);
User.findOne(
{ code: req.body.code, environment: process.env.NODE_ENV },
(err, user) => {
if (user && !err) {
responseBody.found = true;
responseBody.room = user.room;
responseBody.code = user.code;
}
res.status(responseBody.found ? 200 : 401).send(responseBody);
}
);
});
router.post("/signup", async (req, res) => {
var code = Math.floor(Math.random() * 1000000 + 1);
var codeList = await User.find().then((res) => {
var listAux = []
for(r=0;r<res.length;r++) {
listAux.push(res[r].code);
}
return listAux;
});
var repeated = false;
while(true) {
for(c=0;c<codeList.length;c++) {
if (codeList[c] == code.toString()) {
repeated = true;
break;
}
}
if(repeated) {
code = Math.floor(Math.random() * 1000000 + 1);
repeated = false;
} else {
break;
}
}
const newUser = new User(req.body);
newUser.code = code;
newUser.shown_gender = Math.round(Math.random()) == 0 ? "Female" : "Male";
const session = await Session.findOne({
name: req.body.subject,
environment: process.env.NODE_ENV,
});
if (session != null && session.active && !session.running) {
const bodyResponse = {
registrationText:
session.registrationText ||
"You will receive now an email with the next steps.",
};
try {
await newUser.save();
console.log("New user saved: "+JSON.stringify(newUser,null,2));
let transporter = nodemailer.createTransport({
host: "mail.us.es",
port: 587,
requireTLS: true,
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD,
},
});
let info = await transporter.sendMail({
from: '"TwinCode Team 👩🏼💻👨🏽💻" <no-reply@twincode.com>', // sender address
to: newUser.mail, // list of receivers
subject: "Welcome to TwinCode ✔", // Subject line
text: "Welcome to TwinCode", // plain text body
html: `
<h1>Welcome to TwinCode</h1>
<br/>
<p>Your anonymous code to participate in the twincode session is the following: <b>${code}</b></p>
<p>But you can click directly <a href="https://twincode.netlify.app/?code=${code}">https://twincode.netlify.app/?code=${code}</a> for easy access when the session starts.</p><br/>
<p>You will receive detailed instructions at the beginning of the session.</p>`, // html body
});
Logger.monitorLog("Message sent: "+ info.messageId);
res.send(bodyResponse);
res.sendStatus(200);
} catch (e) {
Logger.monitorLog(e);
if (e.name == "ValidationError") {
res.sendStatus(400);
} else if (e.code == 11000) {
res.status(400).send({
code: "DUPLICATE",
message: "User already registered on the session.",
});
} else {
res.send({"error": e})
res.sendStatus(500);
}
}
} else {
res.status(404).send("No active session exists");
}
});
module.exports = router;