Skip to content

Commit 904ac3f

Browse files
committed
Fixed login behavior for connect and edit. Added log out command
1 parent 441d624 commit 904ac3f

File tree

5 files changed

+54
-13
lines changed

5 files changed

+54
-13
lines changed

src/commands/login.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { LoginOrchestrator } from '../orchestrators/login.js';
55

66
export default class Login extends BaseCommand {
77
static description =
8-
`Validate a codify.jsonc/codify.json/codify.yaml file.
8+
`Logins to codify cloud account
99
10-
For more information, visit: https://docs.codifycli.com/commands/validate
10+
For more information, visit: https://docs.codifycli.com/commands/login
1111
`
1212

1313
static flags = {}

src/commands/logout.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import chalk from 'chalk';
2+
3+
import { BaseCommand } from '../common/base-command.js';
4+
import { LoginOrchestrator } from '../orchestrators/login.js';
5+
import { LoginHelper } from '../connect/login-helper.js';
6+
7+
export default class Login extends BaseCommand {
8+
static description =
9+
`Logout of codify cloud account
10+
11+
For more information, visit: https://docs.codifycli.com/commands/logout
12+
`
13+
14+
static flags = {}
15+
16+
static examples = [
17+
'<%= config.bin %> <%= command.id %>',
18+
'<%= config.bin %> <%= command.id %> --path=../../import.codify.jsonc',
19+
]
20+
21+
public async run(): Promise<void> {
22+
await LoginHelper.logout();
23+
console.log(chalk.green('\nSuccessfully logged out.'))
24+
25+
process.exit(0);
26+
}
27+
}

src/connect/login-helper.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ export class LoginHelper {
5454

5555
await fs.mkdir(path.dirname(credentialsPath), { recursive: true });
5656
await fs.writeFile(credentialsPath, JSON.stringify({ accessToken }));
57+
58+
this.instance.isLoggedIn = true;
59+
this.instance.credentials = LoginHelper.decodeToken(accessToken);
60+
}
61+
62+
static async logout() {
63+
try {
64+
const credentialsPath = path.join(os.homedir(), '.codify', 'credentials.json');
65+
await fs.rm(credentialsPath);
66+
} catch {}
5767
}
5868

5969
private static async read(): Promise<Credentials | undefined> {
@@ -63,14 +73,7 @@ export class LoginHelper {
6373
const { accessToken } = JSON.parse(credentialsStr);
6474

6575
await LoginHelper.verifyProjectJWT(accessToken);
66-
const decoded = decodeJwt(accessToken);
67-
68-
return {
69-
accessToken,
70-
email: decoded.email as string,
71-
userId: decoded.sub!,
72-
expiry: decoded.exp!,
73-
}
76+
return LoginHelper.decodeToken(accessToken);
7477
} catch {
7578
return undefined;
7679
}
@@ -79,4 +82,15 @@ export class LoginHelper {
7982
private static async verifyProjectJWT(jwt: string) {
8083
return jwtVerify(jwt, PROJECT_JWKS)
8184
}
85+
86+
private static decodeToken(jwt: string): Credentials {
87+
const decoded = decodeJwt(jwt);
88+
89+
return {
90+
accessToken: jwt,
91+
email: decoded.email as string,
92+
userId: decoded.sub!,
93+
expiry: decoded.exp!,
94+
}
95+
}
8296
}

src/orchestrators/connect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export class ConnectOrchestrator {
1616
static async run(oclifConfig: Config, openBrowser = true, onOpen?: (connectionCode: string) => void) {
1717
const login = LoginHelper.get()?.isLoggedIn;
1818
if (!login) {
19+
console.log('User is not logged in. Attempting to log in...')
1920
await LoginOrchestrator.run();
20-
await LoginHelper.load();
2121
}
2222

2323
this.rootCommand = oclifConfig.options.root;

src/orchestrators/edit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import open from 'open';
44

55
import { DashboardApiClient } from '../api/dashboard/index.js';
66
import { config } from '../config.js';
7-
import { ConnectOrchestrator } from './connect.js';
87
import { LoginHelper } from '../connect/login-helper.js';
8+
import { ConnectOrchestrator } from './connect.js';
99
import { LoginOrchestrator } from './login.js';
1010

1111
export class EditOrchestrator {
@@ -14,8 +14,8 @@ export class EditOrchestrator {
1414
static async run(oclifConfig: Config) {
1515
const login = LoginHelper.get()?.isLoggedIn;
1616
if (!login) {
17+
console.log('User is not logged in. Attempting to log in...')
1718
await LoginOrchestrator.run();
18-
await LoginHelper.load();
1919
}
2020

2121
const defaultDocumentId = await DashboardApiClient.getDefaultDocumentId();

0 commit comments

Comments
 (0)