48hr.email/infrastructure/web/routes/login.js
ClaraCrazy 83a4fac4ab
[Feat]: Show total and historical email count in UI
Enhances user interface by displaying both the current number of emails and the largest UID seen, offering better visibility into historical mailbox activity. Updates backend logic and view templates to support this change, and improves maintainability by centralizing count formatting.
2025-12-26 05:53:06 +01:00

94 lines
No EOL
3.4 KiB
JavaScript

const express = require('express')
const router = new express.Router()
const { check, validationResult } = require('express-validator')
const debug = require('debug')('48hr-email:routes')
const randomWord = require('random-word')
const config = require('../../../application/config')
const Helper = require('../../../application/helper')
const helper = new(Helper)
const purgeTime = helper.purgeTimeElemetBuilder()
router.get('/', async(req, res, next) => {
try {
const mailProcessingService = req.app.get('mailProcessingService')
if (!mailProcessingService) {
throw new Error('Mail processing service not available')
}
debug('Login page requested')
const count = await mailProcessingService.getCount()
const largestUid = await req.app.locals.imapService.getLargestUid()
const totalcount = helper.countElementBuilder(count, largestUid)
debug(`Rendering login page with ${count} total mails`)
res.render('login', {
title: `${config.http.branding[0]} | Your temporary Inbox`,
username: randomWord(),
purgeTime: purgeTime,
domains: helper.getDomains(),
count: count,
totalcount: totalcount,
branding: config.http.branding,
example: config.email.examples.account,
})
} catch (error) {
debug('Error loading login page:', error.message)
console.error('Error while loading login page', error)
next(error)
}
})
router.get('/inbox/random', (req, res, _next) => {
const randomDomain = config.email.domains[Math.floor(Math.random() * config.email.domains.length)]
const inbox = `${randomWord()}@${randomDomain}`
debug(`Generated random inbox: ${inbox}`)
res.redirect(`/inbox/${inbox}`)
})
router.get('/logout', (req, res, _next) => {
/**
* If we ever need a logout sequence, now we can have one!
*/
res.redirect('/')
})
router.post(
'/', [
check('username').isLength({ min: 1 }),
check('domain').isIn(config.email.domains)
],
async(req, res, next) => {
try {
const mailProcessingService = req.app.get('mailProcessingService')
if (!mailProcessingService) {
throw new Error('Mail processing service not available')
}
const errors = validationResult(req)
const count = await mailProcessingService.getCount()
if (!errors.isEmpty()) {
debug(`Login validation failed for ${req.body.username}@${req.body.domain}: ${errors.array().map(e => e.msg).join(', ')}`)
return res.render('login', {
userInputError: true,
title: `${config.http.branding[0]} | Your temporary Inbox`,
purgeTime: purgeTime,
username: randomWord(),
domains: helper.getDomains(),
count: count,
branding: config.http.branding,
})
}
const inbox = `${req.body.username}@${req.body.domain}`
debug(`Login successful, redirecting to inbox: ${inbox}`)
res.redirect(`/inbox/${inbox}`)
} catch (error) {
debug('Error processing login:', error.message)
console.error('Error while processing login', error)
next(error)
}
}
)
module.exports = router