TokenData object if available, null if not found or on error
export function loadTokens(): TokenData | null {
try {
const tokenFilePath = getTokenFilePath();
// Check file existence
if (!fs.existsSync(tokenFilePath)) {
saveLog("No stored tokens found", "DEBUG");
return null;
}
// Read encrypted data
const fileContent = fs.readFileSync(tokenFilePath, "utf8");
const storageData = JSON.parse(fileContent);
// Decrypt and parse
const decryptedData = decrypt(storageData.encryptedData, storageData.iv);
const tokenData = JSON.parse(decryptedData) as TokenData;
saveLog("Spotify tokens loaded from secure storage", "DEBUG");
return tokenData;
} catch (error) {
saveLog(`Failed to load tokens: ${error}`, "ERROR");
return null;
}
}
Retrieves and decrypts authentication tokens