feat: improve token verification with environment variable support

- Add support for DOKPLOY_AUTH_TOKEN and DOKPLOY_URL environment variables
- Enhance error messaging for authentication configuration
- Improve token validation process with more robust error handling
- Update version to v0.2.6
This commit is contained in:
Mauricio Siu
2025-02-23 21:44:04 -06:00
parent 10cfe23763
commit 6d186d550e
2 changed files with 40 additions and 23 deletions

View File

@@ -1,7 +1,7 @@
{
"name": "@dokploy/cli",
"description": "A CLI to manage dokploy server remotely",
"version": "v0.2.4",
"version": "v0.2.6",
"author": "Mauricio Siu",
"licenses": [{
"type": "MIT",

View File

@@ -17,28 +17,46 @@ export default class Verify extends Command {
async run() {
console.log(chalk.blue.bold("\nVerifying Authentication Token"));
if (!fs.existsSync(configPath)) {
this.error(
chalk.red(
"No configuration file found. Please authenticate first using `authenticate` command.",
),
);
}
const configFileContent = fs.readFileSync(configPath, "utf8");
const config = JSON.parse(configFileContent);
const { token, url } = config;
if (!url || !token) {
this.error(
chalk.red(
"Incomplete authentication details. Please authenticate again using `authenticate` command.",
),
);
let token: string;
let url: string;
// Verificar variables de entorno primero
const envToken = process.env.DOKPLOY_AUTH_TOKEN;
const envUrl = process.env.DOKPLOY_URL;
if (envToken && envUrl) {
token = envToken;
url = envUrl;
this.log(chalk.green("Using environment variables for authentication"));
} else {
// Si no hay variables de entorno, verificar archivo de configuración
if (!fs.existsSync(configPath)) {
this.error(
chalk.red(
"No configuration found. Please either:\n" +
"1. Authenticate using `authenticate` command\n" +
"2. Set DOKPLOY_URL and DOKPLOY_AUTH_TOKEN environment variables",
),
);
}
try {
const config = JSON.parse(fs.readFileSync(configPath, "utf8"));
token = config.token;
url = config.url;
this.log(chalk.green("Using configuration file for authentication"));
} catch (error) {
this.error(
chalk.red(
"Invalid configuration file. Please authenticate again using `authenticate` command.",
),
);
}
}
// Validar el token contra el servidor
try {
console.log(`\n${chalk.blue("Validating token...")}`);
console.log(chalk.blue("Validating token with server..."));
const response = await axios.post(
`${url}/api/trpc/auth.verifyToken`,
@@ -52,7 +70,7 @@ export default class Verify extends Command {
);
if (response.data.result.data.json) {
this.log(chalk.green("Token is valid."));
this.log(chalk.green("\n✓ Token is valid"));
} else {
this.error(
chalk.red(
@@ -60,10 +78,9 @@ export default class Verify extends Command {
),
);
}
} catch (error) {
} catch (error: any) {
this.error(
chalk.red(
// @ts-ignore
`Failed to verify token: ${error.message}. Please authenticate again using 'authenticate' command.`,
),
);