Füge myTischtennis-Verbindung hinzu: Implementiere Authentifizierung, Import von TTR-Werten und speichere Verbindungsdaten sicher
This commit is contained in:
10
server/api/cms/mytischtennis.get.js
Normal file
10
server/api/cms/mytischtennis.get.js
Normal file
@@ -0,0 +1,10 @@
|
||||
import { getUserFromToken, hasAnyRole } from '../../utils/auth.js'
|
||||
import { publicConnectionStatus, readMyTischtennisConnection } from '../../utils/mytischtennis-connection.js'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const token = getCookie(event, 'auth_token') || getHeader(event, 'authorization')?.replace(/^Bearer\s+/i, '')
|
||||
const user = token ? await getUserFromToken(token) : null
|
||||
if (!user) throw createError({ statusCode: 401, statusMessage: 'Nicht authentifiziert' })
|
||||
if (!hasAnyRole(user, 'admin', 'vorstand')) throw createError({ statusCode: 403, statusMessage: 'Keine Berechtigung' })
|
||||
return publicConnectionStatus(await readMyTischtennisConnection())
|
||||
})
|
||||
17
server/api/cms/mytischtennis.put.js
Normal file
17
server/api/cms/mytischtennis.put.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import { getUserFromToken, hasAnyRole } from '../../utils/auth.js'
|
||||
import { readMyTischtennisConnection, saveMyTischtennisConnection, publicConnectionStatus } from '../../utils/mytischtennis-connection.js'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const token = getCookie(event, 'auth_token') || getHeader(event, 'authorization')?.replace(/^Bearer\s+/i, '')
|
||||
const user = token ? await getUserFromToken(token) : null
|
||||
if (!user) throw createError({ statusCode: 401, statusMessage: 'Nicht authentifiziert' })
|
||||
if (!hasAnyRole(user, 'admin', 'vorstand')) throw createError({ statusCode: 403, statusMessage: 'Keine Berechtigung' })
|
||||
const body = await readBody(event)
|
||||
if (!/^\S+@\S+\.\S+$/.test(String(body.email || '')) || String(body.password || '').length < 1) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'E-Mail und Passwort sind erforderlich.' })
|
||||
}
|
||||
const previous = await readMyTischtennisConnection()
|
||||
const connection = { ...previous, email: body.email.trim(), password: body.password, association: String(body.association || 'HeTTV'), clubId: String(body.clubId || '43030') }
|
||||
await saveMyTischtennisConnection(connection)
|
||||
return publicConnectionStatus(connection)
|
||||
})
|
||||
19
server/api/cms/mytischtennis/import.post.js
Normal file
19
server/api/cms/mytischtennis/import.post.js
Normal file
@@ -0,0 +1,19 @@
|
||||
import { getUserFromToken, hasAnyRole } from '../../../utils/auth.js'
|
||||
import { importQttrValues } from '../../../utils/qttr-import.js'
|
||||
import { readMyTischtennisConnection, saveMyTischtennisConnection } from '../../../utils/mytischtennis-connection.js'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const token = getCookie(event, 'auth_token') || getHeader(event, 'authorization')?.replace(/^Bearer\s+/i, '')
|
||||
const user = token ? await getUserFromToken(token) : null
|
||||
if (!user) throw createError({ statusCode: 401, statusMessage: 'Nicht authentifiziert' })
|
||||
if (!hasAnyRole(user, 'admin', 'vorstand')) throw createError({ statusCode: 403, statusMessage: 'Keine Berechtigung' })
|
||||
const connection = await readMyTischtennisConnection()
|
||||
if (!connection) throw createError({ statusCode: 409, statusMessage: 'Keine myTischtennis-Verbindung eingerichtet.' })
|
||||
try {
|
||||
const result = await importQttrValues({ connection })
|
||||
return { success: true, rowCount: result.rowCount, importedAt: result.importedAt }
|
||||
} catch (error) {
|
||||
await saveMyTischtennisConnection({ ...connection, lastImportError: String(error.message || error).slice(0, 500) })
|
||||
throw createError({ statusCode: 502, statusMessage: 'TTR-Abruf bei myTischtennis fehlgeschlagen.' })
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user