matterbridge-roborock-vacuum-plugin
Version:
Matterbridge Roborock Vacuum Plugin
40 lines • 1.63 kB
JavaScript
/** Base class for authentication strategies with shared caching logic. */
export class BaseAuthStrategy {
authService;
userDataRepository;
configManager;
logger;
constructor(authService, userDataRepository, configManager, logger) {
this.authService = authService;
this.userDataRepository = userDataRepository;
this.configManager = configManager;
this.logger = logger;
}
/** Format error message from unknown error type. */
formatError(error) {
return error instanceof Error ? error.message : String(error);
}
/** Try to authenticate with cached token if available and not bypassed. */
async tryAuthenticateWithCachedToken(username) {
if (this.configManager.alwaysExecuteAuthentication) {
this.logger.debug('Always execute authentication on startup');
await this.userDataRepository.clearUserData();
return undefined;
}
const savedUserData = await this.userDataRepository.loadUserData(username);
if (!savedUserData) {
this.logger.debug('No saved user data found');
return undefined;
}
this.logger.debug('Found saved user data, attempting to use cached token');
try {
return await this.authService.loginWithCachedToken(username, savedUserData);
}
catch (error) {
this.logger.warn(`Cached token invalid or expired: ${this.formatError(error)}`);
await this.userDataRepository.clearUserData();
return undefined;
}
}
}
//# sourceMappingURL=BaseAuthStrategy.js.map