todofordevs
Version:
CLI for TodoForDevs - A simple task management tool for developers
139 lines (138 loc) • 3.82 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getActiveProject = getActiveProject;
exports.setActiveProject = setActiveProject;
exports.clearActiveProject = clearActiveProject;
exports.getAuthToken = getAuthToken;
exports.setAuthToken = setAuthToken;
exports.clearAuthToken = clearAuthToken;
exports.isAuthenticated = isAuthenticated;
exports.needsTokenRefresh = needsTokenRefresh;
exports.getAuthUser = getAuthUser;
exports.getApiUrl = getApiUrl;
const conf_1 = __importDefault(require("conf"));
// Create a configuration instance
const config = new conf_1.default({
projectName: 'todofordevs',
schema: {
activeProject: {
type: 'object',
properties: {
id: { type: 'string' },
name: { type: 'string' },
},
},
auth: {
type: 'object',
properties: {
token: { type: 'string' },
expiresAt: { type: 'number' },
user: {
type: 'object',
properties: {
id: { type: 'string' },
email: { type: 'string' },
name: { type: 'string' },
},
},
},
},
},
});
/**
* Get the active project from configuration
*/
function getActiveProject() {
return config.get('activeProject');
}
/**
* Set the active project in configuration
*/
function setActiveProject(id, name) {
config.set('activeProject', { id, name });
}
/**
* Clear the active project from configuration
*/
function clearActiveProject() {
config.delete('activeProject');
}
/**
* Get the authentication token from configuration
*/
function getAuthToken() {
return config.get('auth.token');
}
/**
* Set the authentication token in configuration
*/
function setAuthToken(token, user) {
config.set('auth', {
token,
expiresAt: Date.now() + 24 * 60 * 60 * 1000, // 24 hours from now
user,
});
}
/**
* Clear the authentication token from configuration
*/
function clearAuthToken() {
config.delete('auth');
}
/**
* Check if the user is authenticated
*/
function isAuthenticated() {
const token = getAuthToken();
if (!token)
return false;
// Check if the token is expired
const expiresAt = config.get('auth.expiresAt');
if (expiresAt && typeof expiresAt === 'number') {
// If the token expires in less than 5 minutes, consider it expired
const fiveMinutesFromNow = Date.now() + 5 * 60 * 1000;
if (expiresAt < fiveMinutesFromNow) {
return false;
}
}
return true;
}
/**
* Check if the token needs to be refreshed
* Returns true if the token is valid but will expire soon
*/
function needsTokenRefresh() {
const token = getAuthToken();
if (!token)
return false;
// Check if the token will expire soon
const expiresAt = config.get('auth.expiresAt');
if (expiresAt && typeof expiresAt === 'number') {
// If the token expires in less than 1 hour, it needs to be refreshed
const oneHourFromNow = Date.now() + 60 * 60 * 1000;
if (expiresAt < oneHourFromNow) {
return true;
}
}
return false;
}
/**
* Get the authenticated user from configuration
*/
function getAuthUser() {
return config.get('auth.user');
}
/**
* Get the API URL
*/
function getApiUrl() {
// Clear any previously stored API URL
if (config.has('apiUrl')) {
config.delete('apiUrl');
}
return 'https://todofordevs.com/api';
}
exports.default = config;