Skip to content

Commit c6c3679

Browse files
Merge pull request #1 from faintastic/main
Finally make the javascript example official 🙏
2 parents c38bdec + a6bb873 commit c6c3679

File tree

11 files changed

+2391
-291
lines changed

11 files changed

+2391
-291
lines changed

README.md

Lines changed: 169 additions & 291 deletions
Large diffs are not rendered by default.

javascript/.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# dependencies (bun install)
2+
node_modules
3+
4+
# output
5+
out
6+
dist
7+
*.tgz
8+
9+
# code coverage
10+
coverage
11+
*.lcov
12+
13+
# logs
14+
logs
15+
_.log
16+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
17+
18+
# dotenv environment variable files
19+
.env
20+
.env.development.local
21+
.env.test.local
22+
.env.production.local
23+
.env.local
24+
25+
# caches
26+
.eslintcache
27+
.cache
28+
*.tsbuildinfo
29+
30+
# IntelliJ based IDEs
31+
.idea
32+
33+
# Finder (MacOS) folder config
34+
.DS_Store
35+
36+
# Bun runtime (https://bun.sh/)
37+
bun.lock
38+
bun.lockb

javascript/jsconfig.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"compilerOptions": {
3+
// Environment setup & latest features
4+
"lib": ["ESNext"],
5+
"target": "ESNext",
6+
"module": "ESNext",
7+
"moduleDetection": "force",
8+
"jsx": "react-jsx",
9+
"allowJs": true,
10+
11+
// Bundler mode
12+
"moduleResolution": "bundler",
13+
"allowImportingTsExtensions": true,
14+
"verbatimModuleSyntax": true,
15+
"noEmit": true,
16+
17+
// Best practices
18+
"strict": true,
19+
"skipLibCheck": true,
20+
"noFallthroughCasesInSwitch": true,
21+
"noUncheckedIndexedAccess": true,
22+
23+
// Some stricter flags (disabled by default)
24+
"noUnusedLocals": false,
25+
"noUnusedParameters": false,
26+
"noPropertyAccessFromIndexSignature": false
27+
}
28+
}

javascript/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "keyauth-js-example",
3+
"module": "src/index.js",
4+
"type": "module",
5+
"devDependencies": {
6+
"@types/bun": "latest",
7+
"@types/qrcode": "^1.5.5"
8+
},
9+
"peerDependencies": {
10+
"typescript": "^5"
11+
},
12+
"dependencies": {
13+
"discord-interactions": "^4.1.1",
14+
"qrcode": "^1.5.4",
15+
"tweetnacl": "^1.0.3"
16+
}
17+
}

javascript/src/index.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import KeyAuth from "./keyauth"
2+
import { createInterface } from "readline/promises"
3+
4+
const readline = createInterface({
5+
input: process.stdin,
6+
output: process.stdout,
7+
terminal: false
8+
})
9+
10+
const KeyAuthApp = new KeyAuth({
11+
name: "",
12+
ownerid: "",
13+
version: "",
14+
})
15+
16+
async function answer() {
17+
try {
18+
await KeyAuthApp.init()
19+
20+
console.log("[1] Login\n[2] Register\n[3] License\n[4] Upgrade")
21+
22+
const optionRaw = await readline.question("Select an option: ")
23+
const option = parseInt(optionRaw)
24+
25+
let username = "",
26+
password = "",
27+
license = ""
28+
29+
switch (option) {
30+
case 1:
31+
username = await readline.question("Username: ")
32+
password = await readline.question("Password: ")
33+
await KeyAuthApp.login(username, password)
34+
dashboard()
35+
break
36+
37+
case 2:
38+
username = await readline.question("Username: ")
39+
password = await readline.question("Password: ")
40+
license = await readline.question("License: ")
41+
await KeyAuthApp.register(username, password, license)
42+
dashboard()
43+
break
44+
45+
case 3:
46+
license = await readline.question("License: ")
47+
await KeyAuthApp.license(license)
48+
dashboard()
49+
break
50+
51+
case 4:
52+
username = await readline.question("Username: ")
53+
license = await readline.question("License: ")
54+
await KeyAuthApp.upgrade(username, license)
55+
dashboard()
56+
break
57+
58+
default:
59+
console.log("Invalid option selected.")
60+
break
61+
}
62+
} catch (error) {
63+
if (error instanceof Error) {
64+
console.error("An error occurred:", error.message)
65+
} else {
66+
console.error("An unknown error occurred:", error)
67+
}
68+
}
69+
}
70+
71+
answer()
72+
73+
async function dashboard() {
74+
await KeyAuthApp.fetchStats()
75+
console.log("Application data:")
76+
console.log(" App Version: ", KeyAuthApp.app_data?.app_ver)
77+
console.log(" Customer panel: ", KeyAuthApp.app_data?.customer_panel)
78+
console.log(" Number of Keys: ", KeyAuthApp.app_data?.numKeys)
79+
console.log(" Number of Users: ", KeyAuthApp.app_data?.numUsers)
80+
console.log(" Online Users: ", KeyAuthApp.app_data?.onlineUsers)
81+
82+
console.log("\nUser data:")
83+
console.log(" Username: ", KeyAuthApp.user_data?.username)
84+
console.log(" IP Address: ", KeyAuthApp.user_data?.ip)
85+
console.log(" Hardware-id: ", KeyAuthApp.user_data?.hwid)
86+
87+
const subs = KeyAuthApp.user_data?.subscriptions || []
88+
89+
for (let i = 0; i < subs.length; i++) {
90+
const sub = subs[i]
91+
const expiry = new Date(Number(sub.expiry) * 1000)
92+
93+
console.log(
94+
`[${i + 1}/${subs.length}] | Subscription: ${
95+
sub.subscription
96+
} - Expiry: ${expiry.toLocaleString()}`
97+
)
98+
}
99+
100+
console.log(
101+
`Created at: ${new Date(
102+
(KeyAuthApp.user_data?.createdate || 0) * 1000
103+
).toLocaleString()}`
104+
)
105+
console.log(
106+
`Last Login: ${new Date(
107+
(KeyAuthApp.user_data?.lastlogin || 0) * 1000
108+
).toLocaleString()}`
109+
)
110+
console.log(
111+
`Expires: ${new Date(
112+
(KeyAuthApp.user_data?.expires || 0) * 1000
113+
).toLocaleString()}`
114+
)
115+
116+
console.log("\n2-factor authentication:")
117+
console.log("[1] Enable\n[2] Disable")
118+
119+
const optionRaw = await readline.question("Select an option: ")
120+
const option = parseInt(optionRaw)
121+
122+
switch (option) {
123+
case 1:
124+
await KeyAuthApp.enable2fa()
125+
break
126+
case 2:
127+
await KeyAuthApp.disable2fa()
128+
break
129+
default:
130+
console.log("Invalid option selected.")
131+
break
132+
}
133+
134+
console.log("Closing app in 10 seconds...")
135+
await new Promise(resolve => setTimeout(resolve, 10000))
136+
readline.close()
137+
await KeyAuthApp.logout()
138+
process.exit(0)
139+
}

0 commit comments

Comments
 (0)