const router = module.exports = require('express-promise-router')() const ensureUser = require('./middleware/ensureUser') const db = require('../db') const email = require('../utils/email') router.get('/links', ensureUser, async (req, res) => { const links = await db.user.getOpenEmailLinks(req.user.uuid) res.json(links.map(stripLink)) }) router.post('/', ensureUser, async (req, res) => { if(req.user.time_email_confirmed) return res.status(400).json({errors: [{ param: 'email', msg: 'Email address already verified' }]}) await email.sendAccountConfirmation(req.user) res.json({sent: true}) }) router.get('/confirm/:uuid', ensureUser, async (req, res) => { if(!req.query || !req.query.key) return res.redirect('/account/email/invalid') const validLink = await db.user.verifyLoginLink(req.params.uuid, req.query.key) if(!validLink) return res.redirect('/account/email/invalid') await db.user.markLinkUsed(validLink.uuid) await db.user.markEmailVerified(validLink.user_uuid) return res.redirect('/account') }) function stripLink(link) { return { ...link, login_hash: undefined } }