You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.5 KiB
JavaScript
57 lines
1.5 KiB
JavaScript
const router = module.exports = require('express-promise-router')()
|
|
const ensureUser = require('./middleware/ensureUser')
|
|
const db = require('../db')
|
|
|
|
const sendgrid = require('@sendgrid/mail')
|
|
sendgrid.setApiKey(process.env.SENDGRID_KEY)
|
|
|
|
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'
|
|
}]})
|
|
|
|
const confirmUrl = await db.user.createLoginLink(req.user.uuid)
|
|
|
|
const msg = {
|
|
to: req.user.email,
|
|
from: {email: 'registration@email.societyofsocks.us', name: 'Society of Socks'},
|
|
templateId: 'd-33407f1dd1b14b7b84dd779511039c95',
|
|
dynamic_template_data: {
|
|
confirmUrl: confirmUrl
|
|
}
|
|
};
|
|
|
|
await sendgrid.send(msg);
|
|
|
|
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.markLoginLinkUsed(validLink.uuid)
|
|
await db.user.markEmailVerified(validLink.user_uuid)
|
|
|
|
return res.redirect('/account')
|
|
})
|
|
|
|
function stripLink(link) {
|
|
return {
|
|
...link,
|
|
login_hash: undefined
|
|
}
|
|
}
|