Skip to content

Commit 69f8ae1

Browse files
committed
Fixed login bugs.
- Resolve promise when logged in - Fixed login manager bug Added useful log message for connect
1 parent 5976346 commit 69f8ae1

File tree

4 files changed

+51
-21
lines changed

4 files changed

+51
-21
lines changed

src/commands/edit.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { BaseCommand } from '../common/base-command.js';
2+
import { ConnectOrchestrator } from '../orchestrators/connect.js';
3+
4+
export default class Edit extends BaseCommand {
5+
static description =
6+
`Edit a codify.jsonc/codify.json/codify.yaml file.
7+
8+
For more information, visit: https://docs.codifycli.com/commands/validate
9+
`
10+
11+
static flags = {}
12+
13+
static examples = [
14+
'<%= config.bin %> <%= command.id %>',
15+
'<%= config.bin %> <%= command.id %> --path=../../import.codify.jsonc',
16+
]
17+
18+
public async run(): Promise<void> {
19+
const { flags } = await this.parse(Edit)
20+
21+
await ConnectOrchestrator.run();
22+
}
23+
}

src/connect/login-helper.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ export class LoginHelper {
4949

5050
private static async read(): Promise<Credentials | undefined> {
5151
const credentialsPath = path.join(os.homedir(), '.codify', 'credentials.json');
52-
const credentialsStr = await fs.readFile(credentialsPath, 'utf8');
53-
5452
try {
53+
const credentialsStr = await fs.readFile(credentialsPath, 'utf8');
5554
return JSON.parse(credentialsStr);
5655
} catch {
5756
return undefined;

src/orchestrators/connect.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import cors from 'cors';
22
import express, { json } from 'express';
33
import { randomBytes } from 'node:crypto';
44
import open from 'open';
5-
import { WebSocket } from 'ws';
65

76
import { config } from '../config.js';
87
import HttpRouteHandler from '../connect/http-route-handler.js';
@@ -20,6 +19,10 @@ export class ConnectOrchestrator {
2019

2120
const server = app.listen(config.connectServerPort, () => {
2221
open(`http://localhost:3000/connection/success?code=${connectionSecret}`)
22+
console.log(`Open browser window to store code.
23+
24+
If unsuccessful manually enter the code:
25+
${connectionSecret}`)
2326
});
2427

2528
const wsManager = WsServerManager.init(server, connectionSecret)

src/orchestrators/login.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import cors from 'cors';
22
import express, { json } from 'express';
3-
import * as fs from 'node:fs/promises';
4-
import * as os from 'node:os';
5-
import path from 'node:path';
63
import open from 'open';
74

85
import { config } from '../config.js';
9-
import { ajv } from '../utils/ajv.js';
106
import { LoginHelper } from '../connect/login-helper.js';
7+
import { ajv } from '../utils/ajv.js';
118

129
const schema = {
1310
type: 'object',
@@ -22,7 +19,7 @@ const schema = {
2219
type: 'string',
2320
},
2421
expiry: {
25-
type: 'string',
22+
type: 'number',
2623
}
2724
},
2825
additionalProperties: false,
@@ -37,25 +34,33 @@ interface Credentials {
3734
}
3835

3936
export class LoginOrchestrator {
40-
static async run(){
37+
static async run() {
4138
const app = express();
4239

4340
app.use(cors({ origin: config.corsAllowedOrigins }))
4441
app.use(json())
4542

46-
app.post('/', async (req, res) => {
47-
const body = req.body as Credentials;
48-
if (!ajv.validate(schema, body)) {
49-
return res.status(400).send({ message: ajv.errorsText() })
50-
}
43+
const [, server] = await Promise.all([
44+
new Promise<void>((resolve) => {
45+
app.post('/', async (req, res) => {
46+
const body = req.body as Credentials;
47+
48+
if (!ajv.validate(schema, body)) {
49+
return res.status(400).send({ message: ajv.errorsText() })
50+
}
51+
52+
await LoginHelper.save(body);
53+
res.sendStatus(200);
5154

52-
await LoginHelper.save(body);
53-
return res.sendStatus(200);
54-
});
55+
resolve();
56+
});
57+
}),
58+
app.listen(config.loginServerPort, () => {
59+
console.log('Opening CLI auth page...')
60+
open('http://localhost:3000/auth/cli');
61+
})
62+
])
5563

56-
app.listen(config.loginServerPort, () => {
57-
console.log('Opening CLI auth page...')
58-
open('http://localhost:3000/auth/cli');
59-
})
64+
server.close(() => {});
6065
}
6166
}

0 commit comments

Comments
 (0)