Skip to content

Commit 4ea1954

Browse files
authored
break into different command parameters (#9)
* break into different command parameters * set specific versions
1 parent 5ed48f6 commit 4ea1954

File tree

2 files changed

+118
-71
lines changed

2 files changed

+118
-71
lines changed

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
"commander": "11.1.0",
2626
"node": "20.7.0",
2727
"node-fetch": "3.3.2",
28-
"prettier": "^3.0.3",
28+
"prettier": "3.0.3",
2929
"rimraf": "5.0.5"
3030
},
3131
"devDependencies": {
32-
"@typescript-eslint/eslint-plugin": "^6.8.0",
33-
"@typescript-eslint/parser": "^6.8.0",
34-
"eslint": "^8.51.0",
35-
"eslint-plugin-prettier": "^5.0.1",
36-
"typescript": "^5.2.2"
32+
"@typescript-eslint/eslint-plugin": "6.8.0",
33+
"@typescript-eslint/parser": "6.8.0",
34+
"eslint": "8.51.0",
35+
"eslint-plugin-prettier": "5.0.1",
36+
"typescript": "5.2.2"
3737
}
3838
}

src/index.ts

Lines changed: 112 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,117 @@
1-
import { Command } from 'commander'
2-
import chalk from 'chalk'
3-
import DescopeClient from '@descope/node-sdk'
4-
import * as readline from 'readline'
1+
import { Command } from "commander";
2+
import chalk from "chalk";
3+
import DescopeClient from "@descope/node-sdk";
4+
import * as readline from "readline";
55

6-
const program = new Command()
6+
const program = new Command();
77

88
export const getCode = async (query: string) => {
9-
const rl = readline.createInterface({
10-
input: process.stdin,
11-
output: process.stdout
12-
})
13-
return await new Promise((resolve) => {
14-
rl.question(query, (ans) => {
15-
rl.close()
16-
resolve(ans)
17-
})
18-
}
19-
)
20-
}
9+
const rl = readline.createInterface({
10+
input: process.stdin,
11+
output: process.stdout,
12+
});
13+
return await new Promise((resolve) => {
14+
rl.question(query, (ans) => {
15+
rl.close();
16+
resolve(ans);
17+
});
18+
});
19+
};
2120

2221
program
23-
.name('cli-authentication')
24-
.description('Sample app for CLI authentication with Descope')
25-
.version('1.0.0')
26-
.command('login')
27-
.requiredOption(
28-
'-e, --email <email>',
29-
'email of user'
30-
)
31-
.requiredOption(
32-
'-p, --projectId <projectId>',
33-
'Descope Project ID'
34-
)
35-
.action(async (opts) => {
36-
const clientAuth = DescopeClient({ projectId: opts.projectId })
37-
38-
const res = await clientAuth.otp.signUpOrIn.email(opts.email)
39-
if (!res.ok) {
40-
console.log(`Error ${res.error?.errorCode}: ${res.error?.errorDescription}`)
41-
return
42-
}
43-
const code = await getCode(chalk.yellow('Please type code sent by email: '))
44-
const jwt = await clientAuth.otp.verify.email(opts.email, `${code}`)
45-
46-
if (!res.ok) {
47-
console.log(`Error ${res.error?.errorCode}: ${res.error?.errorDescription}`)
48-
return
49-
}
50-
console.log(chalk.green('Code verified successfully.'))
51-
52-
console.log('User logged in')
53-
console.log('**************')
54-
console.log(jwt.data)
55-
console.log()
56-
57-
console.log('User Details (me)')
58-
console.log('**************')
59-
const me = await clientAuth.me(jwt.data?.refreshJwt)
60-
console.log(me.data)
61-
console.log()
62-
63-
console.log('Refreshing...')
64-
const newJwt = await clientAuth.refreshSession(jwt.data?.refreshJwt ?? '')
65-
console.log()
66-
console.log('New Session JWT2:')
67-
console.log(newJwt)
68-
})
69-
70-
program.parse()
22+
.name("CLI Authentication")
23+
.description("Perform authentication actions with Descope Node SDK using CLI")
24+
.version("1.0.0");
25+
26+
program
27+
.command("login")
28+
.description("Login with OTP/Email")
29+
.requiredOption("-e, --email <email>", "email of user")
30+
.requiredOption("-p, --projectId <projectId>", "Descope Project ID")
31+
.action(async (opts) => {
32+
const clientAuth = DescopeClient({ projectId: opts.projectId });
33+
34+
const res = await clientAuth.otp.signUpOrIn.email(opts.email);
35+
if (!res.ok) {
36+
console.log(`Error ${res.error?.errorCode}: ${res.error?.errorDescription}`);
37+
return;
38+
}
39+
const code = await getCode(chalk.yellow("Please type code sent by email: "));
40+
const jwt = await clientAuth.otp.verify.email(opts.email, `${code}`);
41+
42+
if (!res.ok) {
43+
console.log(`Error ${res.error?.errorCode}: ${res.error?.errorDescription}`);
44+
return;
45+
}
46+
console.log(chalk.green("Code verified successfully."));
47+
48+
console.log("User logged in");
49+
console.log("**************");
50+
console.log(jwt.data);
51+
console.log();
52+
});
53+
54+
program
55+
.command("me")
56+
.description("Get user information based on the provided refresh token")
57+
.requiredOption("-r, --refresh <refresh>", "Refresh token")
58+
.requiredOption("-p, --projectId <projectId>", "Descope Project ID")
59+
.action(async (opts) => {
60+
const clientAuth = DescopeClient({ projectId: opts.projectId });
61+
62+
console.log("User Details (me)");
63+
console.log("*****************");
64+
const me = await clientAuth.me(opts.refresh);
65+
console.log(me.data);
66+
console.log();
67+
});
68+
69+
program
70+
.command("validate")
71+
.description("Validate provided session token")
72+
.requiredOption("-s, --session <refresh>", "Session token")
73+
.requiredOption("-p, --projectId <projectId>", "Descope Project ID")
74+
.action(async (opts) => {
75+
const clientAuth = DescopeClient({ projectId: opts.projectId });
76+
77+
console.log("Validating...");
78+
const newJwt = await clientAuth.validateSession(opts.session);
79+
console.log();
80+
console.log("Response:");
81+
console.log(newJwt);
82+
});
83+
84+
program
85+
.command("refresh")
86+
.description("Refresh session using a provided refresh token and get a new session token")
87+
.requiredOption("-r, --refresh <refresh>", "Refresh token")
88+
.requiredOption("-p, --projectId <projectId>", "Descope Project ID")
89+
.action(async (opts) => {
90+
const clientAuth = DescopeClient({ projectId: opts.projectId });
91+
92+
console.log("Refreshing...");
93+
const newJwt = await clientAuth.refreshSession(opts.refresh);
94+
console.log();
95+
console.log("New Session JWT2:");
96+
console.log(newJwt);
97+
});
98+
99+
program
100+
.command("validate-and-refresh")
101+
.description(
102+
"Validate provided session token, and if failed - refresh it and get a new one, using the provided refresh token"
103+
)
104+
.requiredOption("-r, --refresh <refresh>", "Refresh token")
105+
.requiredOption("-s, --session <refresh>", "Session token")
106+
.requiredOption("-p, --projectId <projectId>", "Descope Project ID")
107+
.action(async (opts) => {
108+
const clientAuth = DescopeClient({ projectId: opts.projectId });
109+
110+
console.log("Refreshing...");
111+
const newJwt = await clientAuth.validateAndRefreshSession(opts.session, opts.refresh);
112+
console.log();
113+
console.log("New Session JWT2:");
114+
console.log(newJwt);
115+
});
116+
117+
program.parse();

0 commit comments

Comments
 (0)