Skip to content

Commit

Permalink
fixed other messages
Browse files Browse the repository at this point in the history
  • Loading branch information
cs-raj committed Mar 23, 2024
1 parent 5b109c7 commit b36f64c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 68 deletions.
51 changes: 0 additions & 51 deletions packages/contentstack-auth/messages/index.json

This file was deleted.

2 changes: 1 addition & 1 deletion packages/contentstack-auth/src/commands/auth/whoami.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class WhoamiCommand extends BaseCommand<typeof WhoamiCommand> {
cliux.print(this.email, { color: 'green' });
this.logger.info('Currently logged in user', this.email);
} else {
cliux.error('CLI_AUTH_WHOAMI_FAILED');
cliux.error(messages.CLI_AUTH_WHOAMI_FAILED);
}
} catch (error) {
this.logger.error('whoami error', error.message);
Expand Down
3 changes: 2 additions & 1 deletion packages/contentstack-auth/src/utils/auth-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { cliux, CLIError } from '@contentstack/cli-utilities';
import { User } from '../interfaces';
import { askOTPChannel, askOTP } from './interactive';
import { LoggerService } from '@contentstack/cli-utilities';
import { messages } from '../messages';

/**
* @class
Expand Down Expand Up @@ -54,7 +55,7 @@ class AuthHandler {
if (otpChannel === 'sms') {
try {
await this._client.axiosInstance.post('/user/request_token_sms', { user: loginPayload });
cliux.print('CLI_AUTH_LOGIN_SECURITY_CODE_SEND_SUCCESS');
cliux.print(messages.CLI_AUTH_LOGIN_SECURITY_CODE_SEND_SUCCESS);
} catch (error) {
this.logger.error('Failed to send the security code', error);
reject(new CLIError({ message: 'Failed to login - failed to send the security code' }));
Expand Down
19 changes: 10 additions & 9 deletions packages/contentstack-auth/src/utils/interactive.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { cliux } from '@contentstack/cli-utilities';
import { messages } from '../messages';

export const askPassword = async () => {
return cliux.inquire<string>({
type: 'input',
message: 'CLI_AUTH_LOGIN_ENTER_PASSWORD',
message: messages.CLI_AUTH_LOGIN_ENTER_PASSWORD,
name: 'password',
transformer: (pswd: string) => {
return '*'.repeat(pswd.length);
Expand All @@ -15,7 +16,7 @@ export const askOTPChannel = async (): Promise<string> => {
return cliux.inquire<string>({
type: 'list',
name: 'otpChannel',
message: 'CLI_AUTH_LOGIN_ASK_CHANNEL_FOR_OTP',
message: messages.CLI_AUTH_LOGIN_ASK_CHANNEL_FOR_OTP,
choices: [
{ name: 'Authy App', value: 'authy' },
{ name: 'SMS', value: 'sms' },
Expand All @@ -26,15 +27,15 @@ export const askOTPChannel = async (): Promise<string> => {
export const askOTP = async (): Promise<string> => {
return cliux.inquire({
type: 'input',
message: 'CLI_AUTH_LOGIN_ENTER_SECURITY_CODE',
message: messages.CLI_AUTH_LOGIN_ENTER_SECURITY_CODE,
name: 'tfaToken',
});
};

export const askUsername = async (): Promise<string> => {
return cliux.inquire<string>({
type: 'input',
message: 'CLI_AUTH_LOGIN_ENTER_EMAIL_ADDRESS',
message: messages.CLI_AUTH_LOGIN_ENTER_EMAIL_ADDRESS,
name: 'username',
});
};
Expand All @@ -43,10 +44,10 @@ export const askTokenType = async (): Promise<string> => {
return cliux.inquire<string>({
type: 'list',
name: 'tokenType',
message: 'CLI_SELECT_TOKEN_TYPE',
message: messages.CLI_SELECT_TOKEN_TYPE,
choices: [
{ name: 'Management Token', value: 'management'},
{ name: 'Delivery Token', value: 'delivery'},
]
{ name: 'Management Token', value: 'management' },
{ name: 'Delivery Token', value: 'delivery' },
],
});
}
};
16 changes: 10 additions & 6 deletions packages/contentstack-auth/src/utils/tokens-validation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { messageHandler } from '@contentstack/cli-utilities';
import { LoggerService } from '@contentstack/cli-utilities';
import { messages } from '../messages';
/**
* Validate environment
* @param contentStackClient
Expand All @@ -12,19 +13,22 @@ export const validateEnvironment = async (
apiKey: string,
environment: string,
): Promise<any> => {
const newLogger = new LoggerService(process.cwd(),'cli-log');
const newLogger = new LoggerService(process.cwd(), 'cli-log');
let result: { valid: boolean; message: string };
try {
const validationResult = await contentStackClient.Stack({ api_key: apiKey }).environment(environment).fetch();
newLogger.debug('environment validation result', validationResult);
if (validationResult.name === environment) {
result = { valid: true, message: validationResult };
} else {
result = { valid: false, message: messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_ENVIRONMENT_NAME') };
result = {
valid: false,
message: messageHandler.parse(messages.CLI_AUTH_TOKENS_VALIDATION_INVALID_ENVIRONMENT_NAME),
};
}
} catch (error) {
newLogger.error('validate environment error', error);
result = { valid: false, message: 'CLI_AUTH_TOKENS_VALIDATION_INVALID_ENVIRONMENT_NAME' };
result = { valid: false, message: messages.CLI_AUTH_TOKENS_VALIDATION_INVALID_ENVIRONMENT_NAME };
}
return result;
};
Expand All @@ -36,19 +40,19 @@ export const validateEnvironment = async (
* @returns
*/
export const validateAPIKey = async (contentStackClient: any, apiKey: string): Promise<any> => {
const newLogger = new LoggerService(process.cwd(),'cli-log');
const newLogger = new LoggerService(process.cwd(), 'cli-log');
let result: { valid: boolean; message: string };
try {
const validateAPIKeyResult = await contentStackClient.stack({ api_key: apiKey }).fetch();
newLogger.debug('api key validation result', validateAPIKeyResult);
if (validateAPIKeyResult.api_key === apiKey) {
result = { valid: true, message: validateAPIKeyResult };
} else {
result = { valid: false, message: messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_API_KEY') };
result = { valid: false, message: messageHandler.parse(messages.CLI_AUTH_TOKENS_VALIDATION_INVALID_API_KEY) };
}
} catch (error) {
newLogger.error('validate api key error', error);
result = { valid: false, message: messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_API_KEY') };
result = { valid: false, message: messageHandler.parse(messages.CLI_AUTH_TOKENS_VALIDATION_INVALID_API_KEY) };
}

return result;
Expand Down

0 comments on commit b36f64c

Please sign in to comment.