Added restore session method to recreate the session without needing to create a new one

This commit is contained in:
2025-10-15 14:47:04 -05:00
parent 40943b6055
commit ac05ae423e
2 changed files with 44 additions and 1 deletions

View File

@@ -502,6 +502,49 @@ export class TrytonClient {
};
}
/**
* Restore session using saved session token
* @param sessionToken - Previously saved session string (format: "username:userId:sessionKey")
* @returns Promise<boolean> - True if session is valid
*/
async restoreSession(sessionToken: string): Promise<boolean> {
try {
// Parsear el token guardado
const parts = sessionToken.split(":");
if (parts.length < 3) {
throw new Error("Invalid session token format");
}
const [username, userId, ...sessionKeyParts] = parts;
const sessionKey = sessionKeyParts.join(":"); // Por si el sessionKey tiene ":"
// Establecer la sesión sin hacer login
this.session = sessionToken;
// Verificar que la sesión sigue siendo válida
const proxy = this.connection?.getConnection();
if (!proxy) {
throw new Error("No connection available");
}
// Test de validación de sesión
try {
await proxy.request("common.version", []);
console.log("✅ Sesión restaurada exitosamente");
return true;
} catch (error) {
// Sesión inválida o expirada
this.session = null;
console.log("❌ Sesión inválida o expirada");
return false;
}
} catch (error) {
console.error("❌ Error restaurando sesión:", error);
this.session = null;
return false;
}
}
/**
* Type-safe model operations factory
* Creates a typed interface for specific models