18 lines
1.2 KiB
JavaScript
18 lines
1.2 KiB
JavaScript
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)
|
|
})
|