mirror of
https://github.com/Crazyco-xyz/48hr.email.git
synced 2026-01-09 11:19:36 +01:00
- Update lock removal timer and behaviour - Redirect to previous path on sign-in and out - Fix dashbaord UI and other UX elemets - Lose sanity threlf times
87 lines
3.3 KiB
JavaScript
87 lines
3.3 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}`)
|
|
})
|
|
|
|
// Legacy logout route removed - handled by auth.js
|
|
|
|
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
|