Skip to content

Commit 6d186d5

Browse files
committed
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
1 parent 10cfe23 commit 6d186d5

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@dokploy/cli",
33
"description": "A CLI to manage dokploy server remotely",
4-
"version": "v0.2.4",
4+
"version": "v0.2.6",
55
"author": "Mauricio Siu",
66
"licenses": [{
77
"type": "MIT",

src/commands/verify.ts

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,46 @@ export default class Verify extends Command {
1717
async run() {
1818
console.log(chalk.blue.bold("\nVerifying Authentication Token"));
1919

20-
if (!fs.existsSync(configPath)) {
21-
this.error(
22-
chalk.red(
23-
"No configuration file found. Please authenticate first using `authenticate` command.",
24-
),
25-
);
26-
}
20+
let token: string;
21+
let url: string;
2722

28-
const configFileContent = fs.readFileSync(configPath, "utf8");
29-
const config = JSON.parse(configFileContent);
30-
const { token, url } = config;
23+
// Verificar variables de entorno primero
24+
const envToken = process.env.DOKPLOY_AUTH_TOKEN;
25+
const envUrl = process.env.DOKPLOY_URL;
3126

32-
if (!url || !token) {
33-
this.error(
34-
chalk.red(
35-
"Incomplete authentication details. Please authenticate again using `authenticate` command.",
36-
),
37-
);
27+
if (envToken && envUrl) {
28+
token = envToken;
29+
url = envUrl;
30+
this.log(chalk.green("Using environment variables for authentication"));
31+
} else {
32+
// Si no hay variables de entorno, verificar archivo de configuración
33+
if (!fs.existsSync(configPath)) {
34+
this.error(
35+
chalk.red(
36+
"No configuration found. Please either:\n" +
37+
"1. Authenticate using `authenticate` command\n" +
38+
"2. Set DOKPLOY_URL and DOKPLOY_AUTH_TOKEN environment variables",
39+
),
40+
);
41+
}
42+
43+
try {
44+
const config = JSON.parse(fs.readFileSync(configPath, "utf8"));
45+
token = config.token;
46+
url = config.url;
47+
this.log(chalk.green("Using configuration file for authentication"));
48+
} catch (error) {
49+
this.error(
50+
chalk.red(
51+
"Invalid configuration file. Please authenticate again using `authenticate` command.",
52+
),
53+
);
54+
}
3855
}
3956

57+
// Validar el token contra el servidor
4058
try {
41-
console.log(`\n${chalk.blue("Validating token...")}`);
59+
console.log(chalk.blue("Validating token with server..."));
4260

4361
const response = await axios.post(
4462
`${url}/api/trpc/auth.verifyToken`,
@@ -52,18 +70,17 @@ export default class Verify extends Command {
5270
);
5371

5472
if (response.data.result.data.json) {
55-
this.log(chalk.green("Token is valid."));
73+
this.log(chalk.green("\n✓ Token is valid"));
5674
} else {
5775
this.error(
5876
chalk.red(
5977
"Invalid token. Please authenticate again using `authenticate` command.",
6078
),
6179
);
6280
}
63-
} catch (error) {
81+
} catch (error: any) {
6482
this.error(
6583
chalk.red(
66-
// @ts-ignore
6784
`Failed to verify token: ${error.message}. Please authenticate again using 'authenticate' command.`,
6885
),
6986
);

0 commit comments

Comments
 (0)