Member registration fixed
Some checks failed
Code Analysis and Production Deploy / analyze (push) Failing after 6m17s
Code Analysis and Production Deploy / deploy-production (push) Has been skipped
Code Analysis and Production Deploy / deploy-test (push) Has been skipped

This commit is contained in:
Torsten Schulz (local)
2026-08-12 11:15:07 +02:00
parent 281c25b05d
commit cb89fdd911
15 changed files with 263 additions and 41 deletions

View File

@@ -1,11 +1,13 @@
import { exec } from 'child_process'
import { promisify } from 'util'
import { createHmac } from 'crypto'
import fs from 'fs/promises'
import path from 'path'
import { PDFDocument, StandardFonts, rgb } from 'pdf-lib'
import { getDownloadCookieOptionsWithMaxAge } from '../../utils/cookies.js'
import { sendMembershipEmail as sendMembershipEmailUtil } from '../../utils/email-service.js'
import { getProjectPath, getServerDataPath } from '../../utils/paths.js'
import { createMembershipApplication, removeMembershipApplication } from '../../utils/membership-applications.js'
// const require = createRequire(import.meta.url) // Nicht verwendet
const execAsync = promisify(exec)
@@ -310,9 +312,18 @@ function getDataPath(filename) {
return getServerDataPath(filename)
}
function createMembershipDownloadToken(fileId) {
const issuedAt = Date.now()
const payload = Buffer.from(JSON.stringify({ fileId, issuedAt })).toString('base64url')
const secret = process.env.ENCRYPTION_KEY || 'local_development_encryption_key_change_in_production'
const signature = createHmac('sha256', secret).update(payload).digest('base64url')
return `${payload}.${signature}`
}
// Use central email service
export default defineEventHandler(async (event) => {
let application = null
try {
const body = await readBody(event)
@@ -338,6 +349,10 @@ export default defineEventHandler(async (event) => {
...body,
isVolljaehrig
}
// Persist the pending application before generating files or sending mail.
// This also makes repeated taps and concurrent requests idempotently fail.
application = await createMembershipApplication(data)
// Eindeutige Datei-ID generieren
const timestamp = Date.now()
@@ -630,9 +645,11 @@ export default defineEventHandler(async (event) => {
success: true,
message: 'Beitrittsformular erfolgreich aus Template erzeugt und E-Mail gesendet.',
downloadUrl: `/api/membership/download/${filename}.pdf`,
downloadToken: createMembershipDownloadToken(`${filename}.pdf`),
emailSuccess: emailResult.success,
emailMessage: emailResult.message,
usedTemplate: true
usedTemplate: true,
applicationId: application.id
}
}
@@ -690,8 +707,10 @@ export default defineEventHandler(async (event) => {
success: true,
message: 'Beitrittsformular erfolgreich erstellt und E-Mail gesendet.',
downloadUrl: `/api/membership/download/${filename}.pdf`,
downloadToken: createMembershipDownloadToken(`${filename}.pdf`),
emailSuccess: emailResult.success,
emailMessage: emailResult.message,
applicationId: application.id
}
} catch (latexError) {
@@ -726,16 +745,22 @@ export default defineEventHandler(async (event) => {
success: true,
message: 'Beitrittsformular erfolgreich erstellt und E-Mail gesendet (Fallback-Lösung).',
downloadUrl: `/api/membership/download/${fallbackFilename}`,
downloadToken: createMembershipDownloadToken(fallbackFilename),
emailSuccess: emailResult.success,
emailMessage: emailResult.message,
applicationId: application.id
}
}
} catch (error) {
// A failed generation must not leave a pending application that prevents
// the applicant from trying again.
await removeMembershipApplication(application?.id)
console.error('Fehler beim Generieren des PDFs:', error)
if (error?.statusCode) throw error
throw createError({
statusCode: 500,
statusMessage: 'Fehler beim Generieren des PDFs'
})
}
})
})