@chakresh/kresh 0.1.4 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/commands/login.js +24 -0
- package/src/index.js +8 -0
- package/src/services/auth.js +12 -0
package/package.json
CHANGED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { cliAuthFlow, clearToken, getToken } from '../services/auth.js';
|
|
2
|
+
import { logger } from '../utils/logger.js';
|
|
3
|
+
|
|
4
|
+
export async function loginCommand(options) {
|
|
5
|
+
if (options.flush) {
|
|
6
|
+
clearToken();
|
|
7
|
+
logger.info('Authentication token has been flushed. You are now logged out of the CLI.');
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const existingToken = getToken();
|
|
12
|
+
if (existingToken) {
|
|
13
|
+
logger.info('You are already authenticated. To log in as someone else, run: kresh login --flush');
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
logger.info('Starting browser authentication...');
|
|
19
|
+
await cliAuthFlow();
|
|
20
|
+
// cliAuthFlow already prints success and saves the token
|
|
21
|
+
} catch (error) {
|
|
22
|
+
logger.error('Authentication failed: ' + error.message);
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/index.js
CHANGED
|
@@ -6,6 +6,7 @@ import { searchSkills } from './commands/search.js';
|
|
|
6
6
|
import { listInstalledSkills } from './commands/ls.js';
|
|
7
7
|
import { removeSkill } from './commands/remove.js';
|
|
8
8
|
import { publishSkill } from './commands/publish.js';
|
|
9
|
+
import { loginCommand } from './commands/login.js';
|
|
9
10
|
|
|
10
11
|
const program = new Command();
|
|
11
12
|
|
|
@@ -53,4 +54,11 @@ program
|
|
|
53
54
|
await publishSkill();
|
|
54
55
|
});
|
|
55
56
|
|
|
57
|
+
program
|
|
58
|
+
.command('login [action]')
|
|
59
|
+
.description('Authenticate with the Kresh registry. Use "kresh login flush" to clear your session.')
|
|
60
|
+
.action(async (action) => {
|
|
61
|
+
await loginCommand({ flush: action === 'flush' });
|
|
62
|
+
});
|
|
63
|
+
|
|
56
64
|
program.parse();
|
package/src/services/auth.js
CHANGED
|
@@ -34,6 +34,18 @@ export function setToken(token) {
|
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
export function clearToken() {
|
|
38
|
+
try {
|
|
39
|
+
if (fs.existsSync(CONFIG_FILE)) {
|
|
40
|
+
const config = JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf-8'));
|
|
41
|
+
delete config.token;
|
|
42
|
+
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), 'utf-8');
|
|
43
|
+
}
|
|
44
|
+
} catch (err) {
|
|
45
|
+
logger.error('Failed to clear config: ' + err.message);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
37
49
|
function openBrowser(url) {
|
|
38
50
|
const startCmd = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start' : 'xdg-open';
|
|
39
51
|
exec(`${startCmd} "${url}"`, (err) => {
|