diff --git a/backend/services/myTischtennisService.js b/backend/services/myTischtennisService.js index 127e445..8abc8ac 100644 --- a/backend/services/myTischtennisService.js +++ b/backend/services/myTischtennisService.js @@ -107,6 +107,7 @@ class MyTischtennisService { if (profileResult.success) { accountData.clubId = profileResult.clubId; accountData.clubName = profileResult.clubName; + accountData.fedNickname = profileResult.fedNickname; } } @@ -150,19 +151,49 @@ class MyTischtennisService { throw new HttpError('Kein myTischtennis-Account verknüpft', 404); } - let password = providedPassword; - - // Wenn kein Passwort übergeben wurde, versuche gespeichertes Passwort zu verwenden - if (!password) { - if (!account.savePassword || !account.encryptedPassword) { - throw new HttpError('Kein Passwort gespeichert. Bitte geben Sie Ihr Passwort ein.', 400); - } - password = account.getPassword(); - } - - // Login-Versuch const now = new Date(); account.lastLoginAttempt = now; + + let password = providedPassword; + const hasStoredPassword = !!(account.savePassword && account.encryptedPassword); + const hasValidSession = !!(account.accessToken && account.cookie && account.expiresAt && account.expiresAt > Date.now() / 1000); + + // Wenn kein Passwort angegeben wurde, entweder gespeichertes Passwort oder bestehende Session verwenden + if (!password) { + if (hasStoredPassword) { + password = account.getPassword(); + } else if (hasValidSession) { + // Prüfe, ob bestehende Session noch gültig ist + const profileResult = await myTischtennisClient.getUserProfile(account.cookie); + if (profileResult.success) { + account.lastLoginSuccess = now; + account.clubId = profileResult.clubId || account.clubId; + account.clubName = profileResult.clubName || account.clubName; + account.fedNickname = profileResult.fedNickname || account.fedNickname; + await account.save(); + return { + success: true, + accessToken: account.accessToken, + refreshToken: account.refreshToken, + expiresAt: account.expiresAt, + user: account.userData, + clubId: account.clubId, + clubName: account.clubName + }; + } + // Session ungültig -> Session-Daten zurücksetzen und speichern + account.accessToken = null; + account.refreshToken = null; + account.cookie = null; + account.expiresAt = null; + await account.save(); + throw new HttpError('Kein Passwort gespeichert und Session abgelaufen. Bitte Passwort eingeben.', 400); + } else { + throw new HttpError('Kein Passwort gespeichert. Bitte geben Sie Ihr Passwort ein.', 400); + } + } + + // Login-Versuch mit Passwort const loginResult = await myTischtennisClient.login(account.email, password); if (loginResult.success) { @@ -177,9 +208,9 @@ class MyTischtennisService { const profileResult = await myTischtennisClient.getUserProfile(loginResult.cookie); if (profileResult.success) { - account.clubId = profileResult.clubId; - account.clubName = profileResult.clubName; - account.fedNickname = profileResult.fedNickname; + account.clubId = profileResult.clubId || account.clubId; + account.clubName = profileResult.clubName || account.clubName; + account.fedNickname = profileResult.fedNickname || account.fedNickname; } else { console.error('[myTischtennisService] - Failed to get profile:', profileResult.error); } @@ -211,7 +242,7 @@ class MyTischtennisService { exists: !!account, hasEmail: !!account?.email, hasPassword: !!(account?.savePassword && account?.encryptedPassword), - hasValidSession: !!account?.accessToken && account?.expiresAt > Date.now() / 1000, + hasValidSession: !!(account?.accessToken && account?.cookie && account?.expiresAt && account?.expiresAt > Date.now() / 1000), needsConfiguration: !account || !account.email, needsPassword: !!account && (!account.savePassword || !account.encryptedPassword) }; diff --git a/frontend/src/views/MyTischtennisAccount.vue b/frontend/src/views/MyTischtennisAccount.vue index 5780ad3..cb35258 100644 --- a/frontend/src/views/MyTischtennisAccount.vue +++ b/frontend/src/views/MyTischtennisAccount.vue @@ -16,7 +16,7 @@