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.
44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
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
|
|
}
|
|
}
|