diff --git a/.gitignore b/.gitignore index e920c16..e9aaa70 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,5 @@ node_modules # Optional REPL history .node_repl_history + +.env \ No newline at end of file diff --git a/models/users.js b/models/users.js index 4ba9275..bec29c4 100644 --- a/models/users.js +++ b/models/users.js @@ -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 }, @@ -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(); \ No newline at end of file diff --git a/package.json b/package.json index 57dcdf3..a059801 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/routes/api/users.js b/routes/api/users.js index 9abbf29..11459fb 100644 --- a/routes/api/users.js +++ b/routes/api/users.js @@ -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)=>{ diff --git a/routes/index.js b/routes/index.js index e529a75..77c1c06 100644 --- a/routes/index.js +++ b/routes/index.js @@ -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%40$$w0rd@smtp.gmail.com'); +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 ', to: email, subject: 'Email verfication', text: `Verify your email here : https://nvision.org.in/verify?token=${tk}`, - html: `Verify your email here : Verify` + html: ` +
+
+

+ Here is the last step of your signup.

+ Click me to verify your email

+ + (or) copy and paste this below link in your web browser
+ https://nvision.org.in/verify?token=${tk} +

+ +

+ Thank You,
+ ηvision team +

+
+ ` }; transporter.sendMail(mailOptions, function(err, info){ if (err) return console.log(err); @@ -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); @@ -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)); @@ -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"}); @@ -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(); }); } }); @@ -169,13 +188,26 @@ 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({ @@ -183,6 +215,7 @@ exports = module.exports = function (app) { message: 'Error' }); }); + }); app.post('/events/:id/unregister', (req, res)=>{ @@ -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('/'); }); @@ -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}); } diff --git a/routes/views/about.js b/routes/views/about.js index a25f632..5320345 100644 --- a/routes/views/about.js +++ b/routes/views/about.js @@ -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('/'); diff --git a/routes/views/auth.js b/routes/views/auth.js deleted file mode 100644 index 923f018..0000000 --- a/routes/views/auth.js +++ /dev/null @@ -1,9 +0,0 @@ -var keystone = require('keystone'); - -exports = module.exports = function(req, res) { - - var view = new keystone.View(req, res); - - view.render('auth',{ signupToken:'SOMETHING_dfhiHUHIU823NC38N9QCW8ENCC38C'}); - -} diff --git a/routes/views/events.js b/routes/views/events.js index 104f5f3..3edb31f 100644 --- a/routes/views/events.js +++ b/routes/views/events.js @@ -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}); } diff --git a/routes/views/exhibitions.js b/routes/views/exhibitions.js index a93b4ae..fb67f9c 100644 --- a/routes/views/exhibitions.js +++ b/routes/views/exhibitions.js @@ -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}); } diff --git a/routes/views/index.js b/routes/views/index.js index ee35e2c..f136ecc 100644 --- a/routes/views/index.js +++ b/routes/views/index.js @@ -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}); } diff --git a/routes/views/register.js b/routes/views/register.js index 6bea339..17d6c07 100644 --- a/routes/views/register.js +++ b/routes/views/register.js @@ -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}); } diff --git a/routes/views/sponsors.js b/routes/views/sponsors.js index 41b6e04..9bc46ff 100644 --- a/routes/views/sponsors.js +++ b/routes/views/sponsors.js @@ -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}); } diff --git a/routes/views/workshops.js b/routes/views/workshops.js index 9554e74..a07b8cd 100644 --- a/routes/views/workshops.js +++ b/routes/views/workshops.js @@ -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}); } diff --git a/templates/views/about_mobile.html b/templates/views/about_mobile.html index 49eda76..12ef333 100644 --- a/templates/views/about_mobile.html +++ b/templates/views/about_mobile.html @@ -188,7 +188,7 @@

Updates

  • - + Login diff --git a/templates/views/auth.html b/templates/views/auth.html index b314638..32334f8 100644 --- a/templates/views/auth.html +++ b/templates/views/auth.html @@ -131,7 +131,7 @@

    Updates!

    @@ -224,7 +224,7 @@

    Updates!

  • - + Login @@ -298,7 +298,7 @@

    Updates!

  • - + Login @@ -422,7 +422,7 @@

    Signup

    console.log(body); $("#signup").prop('disabled',true); - $.post('/signup/{{signupToken}}',body,function(res){ + $.post('/signup',body,function(res){ $("#signup").prop('disabled',false); if(res.status){ window.location = res.redirectURL; @@ -453,7 +453,7 @@

    Signup

    console.log(body); $("#login").prop('disabled',true); - $.post('/login',body,function(res){ + $.post('/signin',body,function(res){ $("#login").prop('disabled',false); if(res.status){ window.location = res.redirectURL; diff --git a/templates/views/dashboard.html b/templates/views/dashboard.html index f7f04c7..adc7474 100644 --- a/templates/views/dashboard.html +++ b/templates/views/dashboard.html @@ -343,14 +343,18 @@
    ηvision ID : {{user.nvisionID}}
    College : {{user.college}}
    Phone : {{user.phone}}
    -

    Events Registered

    - {{#each reg}} -
    -
    {{this.event.name}}
    - - Unregister +
    + {{#if n}} +

    Events Registered

    + {{#each reg}} + + {{/each}} + {{else}} +

    No events registered.

    + {{/if}}
    - {{/each}}
    {{/if}}
    diff --git a/templates/views/event.html b/templates/views/event.html index 08ab722..7ff67dd 100644 --- a/templates/views/event.html +++ b/templates/views/event.html @@ -260,7 +260,7 @@

    Updates

  • - + Login @@ -334,7 +334,7 @@

    Updates

  • - + Login diff --git a/templates/views/events.html b/templates/views/events.html index 5fe66ac..7a7ca50 100644 --- a/templates/views/events.html +++ b/templates/views/events.html @@ -185,7 +185,7 @@

    Updates

  • - + Login @@ -259,7 +259,7 @@

    Updates

  • - + Login diff --git a/templates/views/exhibitions.html b/templates/views/exhibitions.html index e0c17ae..3a27f88 100644 --- a/templates/views/exhibitions.html +++ b/templates/views/exhibitions.html @@ -214,7 +214,7 @@

    Updates

  • - + Login @@ -288,7 +288,7 @@

    Updates

  • - + Login diff --git a/templates/views/index.html b/templates/views/index.html index 7c8f5d6..b09ff97 100644 --- a/templates/views/index.html +++ b/templates/views/index.html @@ -280,7 +280,7 @@

    Updates

  • - + Login diff --git a/templates/views/register.html b/templates/views/register.html deleted file mode 100644 index d1a05ea..0000000 --- a/templates/views/register.html +++ /dev/null @@ -1,13 +0,0 @@ -
    - - - -
    -
    -
    - - - - - -
    \ No newline at end of file diff --git a/templates/views/sponsors.html b/templates/views/sponsors.html index 8e4a631..9f0558b 100644 --- a/templates/views/sponsors.html +++ b/templates/views/sponsors.html @@ -243,7 +243,7 @@

    Updates

  • - + Login @@ -317,7 +317,7 @@

    Updates

  • - + Login diff --git a/templates/views/team.html b/templates/views/team.html index 3f08759..1f0557d 100644 --- a/templates/views/team.html +++ b/templates/views/team.html @@ -204,7 +204,7 @@

    Updates

  • - + Login @@ -278,7 +278,7 @@

    Updates

  • - + Login diff --git a/templates/views/workshops.html b/templates/views/workshops.html index aca2bd7..a48105b 100644 --- a/templates/views/workshops.html +++ b/templates/views/workshops.html @@ -237,7 +237,7 @@

    Updates

  • - + Login @@ -311,7 +311,7 @@

    Updates

  • - + Login diff --git a/web.js b/web.js index 3375118..74f9b68 100644 --- a/web.js +++ b/web.js @@ -1,4 +1,5 @@ /* @flow */ +require('dotenv').config(); const keystone = require('keystone'); const cons = require('consolidate');