@snapcommit/cli 3.8.1 → 3.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/auth.js +34 -19
- package/package.json +1 -1
package/dist/lib/auth.js
CHANGED
|
@@ -19,6 +19,9 @@ const CONFIG_DIR = path_1.default.join(os_1.default.homedir(), '.snapcommit');
|
|
|
19
19
|
const AUTH_FILE = path_1.default.join(CONFIG_DIR, 'auth.json');
|
|
20
20
|
// API URL - defaults to production, can be overridden for development
|
|
21
21
|
const API_BASE_URL = process.env.SNAPCOMMIT_API_URL || 'https://snapcommit.dev';
|
|
22
|
+
// Token validation cache
|
|
23
|
+
let lastValidationTime = 0;
|
|
24
|
+
const VALIDATION_INTERVAL = 60 * 60 * 1000; // 1 hour in milliseconds
|
|
22
25
|
/**
|
|
23
26
|
* Ensure config directory exists
|
|
24
27
|
*/
|
|
@@ -167,28 +170,39 @@ async function ensureAuth() {
|
|
|
167
170
|
if (isAuthenticated()) {
|
|
168
171
|
const config = getAuthConfig();
|
|
169
172
|
if (config) {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
173
|
+
const now = Date.now();
|
|
174
|
+
const timeSinceLastValidation = now - lastValidationTime;
|
|
175
|
+
// Only validate if more than 1 hour has passed since last validation
|
|
176
|
+
if (timeSinceLastValidation > VALIDATION_INTERVAL) {
|
|
177
|
+
// CRITICAL: Verify token with server every hour
|
|
178
|
+
try {
|
|
179
|
+
const result = await verifyToken(config.token);
|
|
180
|
+
if (result.valid) {
|
|
181
|
+
// Token is still valid!
|
|
182
|
+
lastValidationTime = now; // Update last validation time
|
|
183
|
+
return config;
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
// Token is invalid (user deleted, subscription expired, etc.)
|
|
187
|
+
console.log(chalk_1.default.yellow('\n⚠️ Your authentication token is no longer valid.'));
|
|
188
|
+
console.log(chalk_1.default.gray('This could mean:'));
|
|
189
|
+
console.log(chalk_1.default.gray(' • Your subscription has expired'));
|
|
190
|
+
console.log(chalk_1.default.gray(' • Your account was deleted'));
|
|
191
|
+
console.log(chalk_1.default.gray(' • The token was revoked\n'));
|
|
192
|
+
clearAuth();
|
|
193
|
+
lastValidationTime = 0; // Reset validation timer
|
|
194
|
+
// Fall through to re-prompt
|
|
195
|
+
}
|
|
176
196
|
}
|
|
177
|
-
|
|
178
|
-
//
|
|
179
|
-
console.log(chalk_1.default.yellow('\n⚠️
|
|
180
|
-
console.log(chalk_1.default.gray('
|
|
181
|
-
|
|
182
|
-
console.log(chalk_1.default.gray(' • Your account was deleted'));
|
|
183
|
-
console.log(chalk_1.default.gray(' • The token was revoked\n'));
|
|
184
|
-
clearAuth();
|
|
185
|
-
// Fall through to re-prompt
|
|
197
|
+
catch (error) {
|
|
198
|
+
// Network error - allow offline usage with cached token
|
|
199
|
+
console.log(chalk_1.default.yellow('\n⚠️ Could not verify token (offline mode)'));
|
|
200
|
+
console.log(chalk_1.default.gray('Using cached credentials. Some features may be limited.\n'));
|
|
201
|
+
return config;
|
|
186
202
|
}
|
|
187
203
|
}
|
|
188
|
-
|
|
189
|
-
//
|
|
190
|
-
console.log(chalk_1.default.yellow('\n⚠️ Could not verify token (offline mode)'));
|
|
191
|
-
console.log(chalk_1.default.gray('Using cached credentials. Some features may be limited.\n'));
|
|
204
|
+
else {
|
|
205
|
+
// Token was validated recently (within the last hour) - skip validation
|
|
192
206
|
return config;
|
|
193
207
|
}
|
|
194
208
|
}
|
|
@@ -196,6 +210,7 @@ async function ensureAuth() {
|
|
|
196
210
|
// No valid token - prompt for authentication
|
|
197
211
|
const success = await promptAuth();
|
|
198
212
|
if (success) {
|
|
213
|
+
lastValidationTime = Date.now(); // Set validation time after successful auth
|
|
199
214
|
return getAuthConfig();
|
|
200
215
|
}
|
|
201
216
|
return null;
|