fix 3
This commit is contained in:
@@ -2,6 +2,7 @@ import { Router } from 'express'
|
||||
import { config } from '../config.js'
|
||||
import {
|
||||
getCustomerByAppwriteUserId,
|
||||
getCustomerByEmail,
|
||||
getPortalAccessByCustomerId,
|
||||
updateDocument,
|
||||
} from '../services/appwriteAdmin.js'
|
||||
@@ -28,10 +29,15 @@ function sanitizeCustomer(customer) {
|
||||
}
|
||||
}
|
||||
|
||||
async function validatePortalAccess(appwriteUserId) {
|
||||
const customer = await getCustomerByAppwriteUserId(appwriteUserId)
|
||||
async function validatePortalAccess(appwriteUserId, email) {
|
||||
let customer = await getCustomerByAppwriteUserId(appwriteUserId)
|
||||
if (!customer && email) {
|
||||
customer = await getCustomerByEmail(email)
|
||||
}
|
||||
if (!customer) {
|
||||
const error = new Error('Kein Kundenkonto für diesen Login gefunden.')
|
||||
const error = new Error(
|
||||
`Kein Kundenkonto für diesen Login gefunden. Im Ticketsystem customers.appwriteUserId auf "${appwriteUserId}" setzen (E-Mail: ${email}).`
|
||||
)
|
||||
error.status = 403
|
||||
throw error
|
||||
}
|
||||
@@ -59,23 +65,6 @@ async function validatePortalAccess(appwriteUserId) {
|
||||
return { customer, portalAccess }
|
||||
}
|
||||
|
||||
const DEBUG_LOG = (location, message, data, hypothesisId) => {
|
||||
// #region agent log
|
||||
fetch('http://127.0.0.1:7281/ingest/30e8e71c-b377-4e72-84f9-593826c6d234', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', 'X-Debug-Session-Id': '80bbfc' },
|
||||
body: JSON.stringify({
|
||||
sessionId: '80bbfc',
|
||||
location,
|
||||
message,
|
||||
data,
|
||||
hypothesisId,
|
||||
timestamp: Date.now(),
|
||||
}),
|
||||
}).catch(() => {})
|
||||
// #endregion
|
||||
}
|
||||
|
||||
router.post('/login', async (req, res) => {
|
||||
const { email, password } = req.body || {}
|
||||
if (!email || !password) {
|
||||
@@ -84,12 +73,7 @@ router.post('/login', async (req, res) => {
|
||||
|
||||
try {
|
||||
const user = await loginWithAppwrite(email.trim(), password)
|
||||
DEBUG_LOG('auth.js:login', 'appwrite user ok', { userId: user.$id }, 'H3')
|
||||
const { customer, portalAccess } = await validatePortalAccess(user.$id)
|
||||
DEBUG_LOG('auth.js:login', 'portal validation ok', {
|
||||
customerId: customer.$id,
|
||||
portalAccessEnabled: Boolean(customer.portalAccessEnabled),
|
||||
}, 'H4')
|
||||
const { customer, portalAccess } = await validatePortalAccess(user.$id, email.trim())
|
||||
|
||||
setPortalSession(res, {
|
||||
customerId: customer.$id,
|
||||
@@ -109,10 +93,12 @@ router.post('/login', async (req, res) => {
|
||||
return res.json({ success: true, customer: sanitizeCustomer(customer) })
|
||||
} catch (err) {
|
||||
const status = err.status || 500
|
||||
DEBUG_LOG('auth.js:login', 'login failed', {
|
||||
status,
|
||||
message: err?.message?.slice(0, 120),
|
||||
}, status === 403 ? 'H4' : status === 401 ? 'H1' : 'H5')
|
||||
if (err?.message?.includes('not authorized')) {
|
||||
return res.status(500).json({
|
||||
error:
|
||||
'Server-Konfiguration: APPWRITE_API_KEY benötigt databases.read für woms-database (customers, customerPortalAccess).',
|
||||
})
|
||||
}
|
||||
return res.status(status).json({ error: err.message || 'Anmeldung fehlgeschlagen' })
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user