Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
prateekkumarweb committed Dec 24, 2016
1 parent ec3a878 commit ed9bf51
Show file tree
Hide file tree
Showing 25 changed files with 137 additions and 95 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ node_modules

# Optional REPL history
.node_repl_history

.env
11 changes: 10 additions & 1 deletion models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ let User = new keystone.List('User', {
nodelete: true
});

function pad(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}

User.add({
name: { type: Types.Name, required: true, index: true },
nvisionID: {type: Types.Text },
userid: {type: Types.Number},
email: { type: Types.Email, initial: true, required: true, index: true, unique: true },
password: { type: Types.Password, required: true, initial: true },
Expand All @@ -24,6 +29,10 @@ User.add({

User.schema.plugin(autoIncrement.plugin, {model: 'User', field: 'userid'});

User.schema.virtual('nvisionID').get(function(){
return 'NVISION17'+pad(this.userid,4);
});

User.relationship({path: 'registrations', ref: 'Registration', refPath: 'user'});

User.register();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"dependencies": {
"async": "^2.1.2",
"consolidate": "^0.14.5",
"dotenv": "^2.0.0",
"handlebars": "^4.0.6",
"jsonwebtoken": "^7.2.1",
"keystone": "latest",
Expand Down
28 changes: 21 additions & 7 deletions routes/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,31 @@ handlers.getEvent = (req, res)=>{
};

handlers.registerEvent = (req, res)=>{
new Registration.model({
if (!req.decoded._doc.emailVerified) {
return res.json({error: 'Email not verified'});
}
Registration.model.findOne({
event: req.params.id,
user: req.decoded._doc._id
}).save(function(err, user){
if (err) {
res.json({error: 'Registration failed'});
}
else {
res.json(user);
}).then((user)=>{
if (user) {
return res.json(user);
}
new Registration.model({
event: req.params.id,
user: req.decoded._doc._id
}).save(function(err, user){
if (err) {
res.json({error: 'Registration failed'});
}
else {
res.json(user);
}
});
}, err=>{

});

};

handlers.deleteEvent = (req, res)=>{
Expand Down
95 changes: 64 additions & 31 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,38 @@ User = keystone.list('User'),
Registration = keystone.list('Registration');

var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport('smtps://nvision2k17%40gmail.com:p%[email protected]');
var transporter = nodemailer.createTransport(process.env.SMTP_EMAIL);
var randtoken = require('rand-token');

function pad(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}

function sendVEmail(tk, email, cb) {
var mailOptions = {
from: 'nvision 2017 <[email protected]>',
to: email,
subject: 'Email verfication',
text: `Verify your email here : https://nvision.org.in/verify?token=${tk}`,
html: `Verify your email here : <a href="https://nvision.org.in/verify?token=${tk}">Verify</a>`
html: `
<div style="width:100%;background-color:#072347;padding:3em 0 6em 0">
<div style="
background-image:url(https://nvision.org.in/img/nvision.png);
height:10em;
background-repeat: no-repeat;
background-position: center;
background-size: contain;
"></div>
<p style="color:#fff;font-size:1.2em;text-align:center">
Here is the last step of your signup.<br><br>
<a href="https://nvision.org.in/verify?token=${tk}" style="text-decoration: none;background-color:#00ccff;color:#072347;padding:6px;border-radius:3px">Click me</a> to verify your email<br><br>
(or) copy and paste this below link in your web browser<br>
<a href="https://nvision.org.in/verify?token=${tk}" style="color:#00ccff">https://nvision.org.in/verify?token=${tk}</a>
</p>
<p style="color:#fff;font-size:1.2em;text-align:center">
Thank You,<br>
&eta;vision team
</p>
</div>
`
};
transporter.sendMail(mailOptions, function(err, info){
if (err) return console.log(err);
Expand Down Expand Up @@ -73,7 +89,6 @@ exports = module.exports = function (app) {
app.get('/mobilemaking', (req, res) => {
res.redirect('/mobileMaking');
});
app.get('/auth', routes.views.auth);
app.get('/about', routes.views.about);
app.get('/', routes.views.index);
app.get('/sponsors', routes.views.sponsors);
Expand All @@ -95,9 +110,11 @@ exports = module.exports = function (app) {
Registration.model.findOne({event: e._id, user: req.user._id}).then(reg=>{
if (reg) e.registered = true;
else e.registered = false;
e.user = req.user;
view.render('event', e);
}, err=>{
e.registered = false;
e.user = req.user;
view.render('event', e);
});
}, e => res.err(e));
Expand All @@ -108,28 +125,31 @@ exports = module.exports = function (app) {
email: req.body.email,
password: req.body.password
}, req, res, user=>{
if (!user) res.redirect('/signin');
else res.redirect('/dashboard');
}, err=>{res.redirect('/signin')});
if (!user) res.json({status: false, message: 'Invalid credentials'});
else res.json({status: true, redirectURL: '/dashboard'});
}, err=>{res.json({status: false, message: 'Invalid credentials'});});
});

app.post('/signup', (req, res) => {
var tk = randtoken.generate(64);
var i = req.body.name.indexOf(' ');
new User.model({
name: { first: req.body.first, last: req.body.last },
name: { first: req.body.name.substr(0,i), last: req.body.name.substr(i) },
email: req.body.email,
password: req.body.password,
college: req.body.college,
phone: req.body.phone,
canAccessKeystone: false,
emailVerified: false,
verificationToken: tk
}).save().then((user)=>{
var token = jwt.sign({token:token}, tokenSecret, {expiresIn: 900});
var token = jwt.sign({token:tk}, tokenSecret, {expiresIn: 900});
sendVEmail(token, req.body.email);
keystone.session.signin({
email: req.body.email,
password: req.body.password
}, req, res, (user)=>{
return res.json({status: true, verified: false, message: 'Email verification email sent'});
return res.json({status: true, verified: false, redirectURL: '/dashboard', message: 'A verification email sent'});
}, (err) => res.json({status: false, message: "Auth failed"}));
}, (err)=>{
res.json({status: false, message: "Auth failed"});
Expand All @@ -139,24 +159,23 @@ exports = module.exports = function (app) {
app.get('/verify', (req, res)=>{
var token = req.query.token;
if (!token) {
return res.send('error');
return res.notfound();
}
jwt.verify(token, tokenSecret, function(err, decoded){
if (err) {
return res.send('error');
return res.notfound();
}
else {
User.model.findOne({emailVerified: false, verificationToken: decoded.token}).then(user=>{
if (!user) return res.send('Error');
user.emailVerified = true;
user.nvisionID = 'NVISION17'+pad(user.userid,4);
user.save().then(usr=>{
res.send('verified');
res.redirect('/dashboard');
}, e=>{
res.send('Error');
res.redirect('/dashboard');
});
}, err=>{
res.send('error');
res.notfound();
});
}
});
Expand All @@ -169,20 +188,34 @@ exports = module.exports = function (app) {
if (!req.user.emailVerified) {
return res.json({status: false, message: 'Email not verified'});
}
new Registration.model({
event: req.params.id,
user: req.user._id
}).save().then(reg=>{
res.json({
status: true,
message: 'Registered'
Registration.model.findOne({event: req.params.id, user: req.user._id}).then((user)=>{
if (user) {
return res.json({
status: true,
message: 'Registered'
});
}
new Registration.model({
event: req.params.id,
user: req.user._id
}).save().then(reg=>{
res.json({
status: true,
message: 'Registered'
});
}, err=>{
res.json({
status: false,
message: 'Error'
});
});
}, err=>{
res.json({
status: false,
message: 'Error'
});
});

});

app.post('/events/:id/unregister', (req, res)=>{
Expand All @@ -208,10 +241,10 @@ exports = module.exports = function (app) {
return res.redirect('/signin');
}
if (!req.user.emailVerified) {
return view.render('dashboard', {emailnv:true});
return view.render('dashboard', {emailnv:true, user:req.user});
}
Registration.model.find({user: req.user._id}).populate('event').exec().then(r=>{
return view.render('dashboard', {reg:r, user:req.user});
return view.render('dashboard', {reg:r, n:r.length, user:req.user});
}, e=>{
return res.redirect('/');
});
Expand All @@ -222,7 +255,7 @@ exports = module.exports = function (app) {
return res.json({status:false, message: 'Auth failed'});
}
if (!req.emailVerified) {
var token = jwt.sign({toen:req.user.verificationToken}, tokenSecret, {expiresIn: 900});
var token = jwt.sign({token:req.user.verificationToken}, tokenSecret, {expiresIn: 900});
sendVEmail(token, req.user.email);
return res.json({status:true});
}
Expand Down
2 changes: 1 addition & 1 deletion routes/views/about.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports = module.exports = function(req, res) {

var view = new keystone.View(req, res);
if (require('../../lib/detectmobilebrowser')(req))
view.render('about_mobile');
view.render('about_mobile', {user: req.user});
else
res.redirect('/');

Expand Down
9 changes: 0 additions & 9 deletions routes/views/auth.js

This file was deleted.

2 changes: 1 addition & 1 deletion routes/views/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ exports = module.exports = function(req, res) {

var view = new keystone.View(req, res);

view.render('events');
view.render('events', {user: req.user});

}
2 changes: 1 addition & 1 deletion routes/views/exhibitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ exports = module.exports = function(req, res) {

var view = new keystone.View(req, res);

view.render('exhibitions');
view.render('exhibitions', {user: req.user});

}
4 changes: 2 additions & 2 deletions routes/views/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ exports = module.exports = function(req, res) {

var view = new keystone.View(req, res);
if (require('../../lib/detectmobilebrowser')(req))
view.render('index_mobile');
view.render('index_mobile', {user: req.user});
else
view.render('index');
view.render('index', {user: req.user});

}
2 changes: 1 addition & 1 deletion routes/views/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ exports = module.exports = function(req, res) {

var view = new keystone.View(req, res);

view.render('register');
view.render('auth', {user: req.user});

}
2 changes: 1 addition & 1 deletion routes/views/sponsors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ exports = module.exports = function(req, res) {

var view = new keystone.View(req, res);

view.render('sponsors');
view.render('sponsors', {user: req.user});

}
2 changes: 1 addition & 1 deletion routes/views/workshops.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ exports = module.exports = function(req, res) {

var view = new keystone.View(req, res);

view.render('workshops');
view.render('workshops', {user: req.user});

}
2 changes: 1 addition & 1 deletion templates/views/about_mobile.html
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ <h3><b>Updates</b></h3>
</ul>
</li>
<li>
<a href="/auth" class="btnfos">
<a href="/signin" class="btnfos">
<svg>
<rect x="0" y="0" fill="none" width="100%" height="100%" />
</svg>Login</a>
Expand Down
Loading

0 comments on commit ed9bf51

Please sign in to comment.