-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathlogin.ts
More file actions
97 lines (83 loc) · 3.16 KB
/
login.ts
File metadata and controls
97 lines (83 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { AxiosError } from "axios";
import { Flags, Command } from "@oclif/core";
import { outro, password, text } from "@clack/prompts";
import { getMessageInRedColor, replaceHTML } from "../../utils/utils.js";
import { SDKClient } from "../../client-utils/sdk-client.js";
import { getAuthInfo } from "../../client-utils/auth-manager.js";
export default class Login extends Command {
static description = "Login using your APIMatic credentials or an API Key";
static examples = [
`$ apimatic auth:login
Enter your registered email: apimatic-user@gmail.com
Please enter your password: *********
You have successfully logged into APIMatic
`,
`$ apimatic auth:login --auth-key=xxxxxx
Authentication key successfully set`
];
static flags = {
"auth-key": Flags.string({ default: "", description: "Set authentication key for all commands" })
};
async run() {
const { flags } = await this.parse(Login);
const configDir: string = this.config.configDir;
try {
// Check if already logged in
const storedAuthInfo = await getAuthInfo(configDir);
if (storedAuthInfo && storedAuthInfo.authKey) {
if (storedAuthInfo.email) {
return this.log(
`You are already logged in as '${storedAuthInfo.email}'. Use auth:logout to logout before logging in again.`
);
}
return this.log(
`You are already logged in with authentication key. Use auth:logout to logout before logging in again.`
);
}
const client: SDKClient = SDKClient.getInstance();
// If user is setting auth key
if (flags["auth-key"]) {
const response = client.setAuthKey(flags["auth-key"], configDir);
return this.log(response);
}
// If user logs in with email and password
const email = await text({
message: "Enter your registered email:",
validate: (input) => {
if (!input) {
return getMessageInRedColor("Email is required.");
}
const emailRegex =
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
if (!emailRegex.test(input)) {
return getMessageInRedColor("Please enter a valid email address.");
}
}
});
const pass = await password({
message: "Please enter your password:",
validate: (input) => {
if (!input) {
return getMessageInRedColor("Password is required.");
}
}
});
const response: string = await client.login(email as string, pass as string, configDir);
outro(response);
} catch (error) {
if (error && (error as AxiosError).response) {
const apiError = error as AxiosError;
const apiResponse = apiError.response;
if (apiResponse) {
const responseData = apiResponse.data;
if (apiResponse.status === 403 && responseData) {
return this.error(replaceHTML(JSON.stringify(responseData)));
} else {
return this.error(apiError.message);
}
}
}
this.error((error as Error).message);
}
}
}