fix1
This commit is contained in:
@@ -59,6 +59,23 @@ async function validatePortalAccess(appwriteUserId) {
|
|||||||
return { customer, portalAccess }
|
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) => {
|
router.post('/login', async (req, res) => {
|
||||||
const { email, password } = req.body || {}
|
const { email, password } = req.body || {}
|
||||||
if (!email || !password) {
|
if (!email || !password) {
|
||||||
@@ -67,7 +84,12 @@ router.post('/login', async (req, res) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const user = await loginWithAppwrite(email.trim(), password)
|
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)
|
const { customer, portalAccess } = await validatePortalAccess(user.$id)
|
||||||
|
DEBUG_LOG('auth.js:login', 'portal validation ok', {
|
||||||
|
customerId: customer.$id,
|
||||||
|
portalAccessEnabled: Boolean(customer.portalAccessEnabled),
|
||||||
|
}, 'H4')
|
||||||
|
|
||||||
setPortalSession(res, {
|
setPortalSession(res, {
|
||||||
customerId: customer.$id,
|
customerId: customer.$id,
|
||||||
@@ -87,6 +109,10 @@ router.post('/login', async (req, res) => {
|
|||||||
return res.json({ success: true, customer: sanitizeCustomer(customer) })
|
return res.json({ success: true, customer: sanitizeCustomer(customer) })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const status = err.status || 500
|
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')
|
||||||
return res.status(status).json({ error: err.message || 'Anmeldung fehlgeschlagen' })
|
return res.status(status).json({ error: err.message || 'Anmeldung fehlgeschlagen' })
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,18 +1,65 @@
|
|||||||
import { createUserClient } from './appwriteAdmin.js'
|
import { createUserClient } from './appwriteAdmin.js'
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
export async function loginWithAppwrite(email, password) {
|
export async function loginWithAppwrite(email, password) {
|
||||||
const { client, account } = createUserClient()
|
const { client, account } = createUserClient()
|
||||||
|
|
||||||
|
let session
|
||||||
try {
|
try {
|
||||||
await account.createEmailPasswordSession(email, password)
|
session = await account.createEmailPasswordSession(email, password)
|
||||||
|
DEBUG_LOG('appwriteClient.js:session', 'createEmailPasswordSession ok', {
|
||||||
|
hasSecret: Boolean(session?.secret),
|
||||||
|
sessionId: session?.$id || null,
|
||||||
|
}, 'H1')
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
DEBUG_LOG('appwriteClient.js:session', 'createEmailPasswordSession fail', {
|
||||||
|
code: err?.code,
|
||||||
|
type: err?.type,
|
||||||
|
message: err?.message?.slice(0, 120),
|
||||||
|
}, 'H1')
|
||||||
const message = err?.message || 'Anmeldung fehlgeschlagen'
|
const message = err?.message || 'Anmeldung fehlgeschlagen'
|
||||||
const error = new Error(message)
|
const error = new Error(message)
|
||||||
error.status = 401
|
error.status = 401
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await account.get()
|
if (session?.secret) {
|
||||||
|
client.setSession(session.secret)
|
||||||
|
DEBUG_LOG('appwriteClient.js:setSession', 'setSession applied', { hasSessionHeader: true }, 'H2')
|
||||||
|
} else {
|
||||||
|
DEBUG_LOG('appwriteClient.js:setSession', 'no session.secret', {}, 'H2')
|
||||||
|
}
|
||||||
|
|
||||||
|
let user
|
||||||
|
try {
|
||||||
|
user = await account.get()
|
||||||
|
DEBUG_LOG('appwriteClient.js:get', 'account.get ok', { userId: user?.$id || null }, 'H2')
|
||||||
|
} catch (err) {
|
||||||
|
DEBUG_LOG('appwriteClient.js:get', 'account.get fail', {
|
||||||
|
code: err?.code,
|
||||||
|
message: err?.message?.slice(0, 120),
|
||||||
|
}, 'H2')
|
||||||
|
const message = err?.message || 'Anmeldung fehlgeschlagen'
|
||||||
|
const error = new Error(message)
|
||||||
|
error.status = err?.message?.includes('scopes') ? 401 : 500
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await account.deleteSession('current')
|
await account.deleteSession('current')
|
||||||
|
|||||||
Reference in New Issue
Block a user