const express = require('express'); const verifyTokenApp = express.Router(); const libs = `${process.cwd()}/libs`; const log = require(`${libs}/log`)(module); const VerificationToken = require(`${libs}/models/verificationToken`); const User = require(`${libs}/models/user`); verifyTokenApp.get('/:token', (req, res, next) => { let token = req.params.token; VerificationToken.findOne({token: token}, (err, vToken) => { if(err) { log.error(err); next(err); } if(!token) { // TODO: generate new verification token } User.findById(vToken.userId, (err, user) => { if(err) { log.error(err); next(err); } user.verified = true; user.save((err) => { if(err) { log.error(err); next(err); } }); VerificationToken.remove({_id: vToken._id}, (err) => { if(err) { log.error(err); next(err); } }); res.json({msg: 'User verified'}); }); }); }); module.exports = verifyTokenApp;