Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions packages/contentstack-utilities/src/auth-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import dotenv from 'dotenv';
import open from 'open';
import http from 'http';
import url from 'url';
import { LoggerService } from './logger';
import { handleAndLogError } from './logger/log';
import managementSDKClient, { ContentstackClient } from './contentstack-management-sdk';
import { formatError } from './helpers';

Expand Down Expand Up @@ -33,7 +33,6 @@ class AuthHandler {
private authorisationTypeOAUTHValue: string;
private authorisationTypeAUTHValue: string;
private allAuthConfigItems: any;
private logger: any;
private oauthHandler: any;
private managementAPIClient: ContentstackClient;
private isRefreshingToken: boolean = false; // Flag to track if a refresh operation is in progress
Expand Down Expand Up @@ -98,9 +97,6 @@ class AuthHandler {
}
return cma;
}
initLog() {
this.logger = new LoggerService(process.cwd(), 'cli-log');
}
async setOAuthBaseURL() {
if (configHandler.get('region')['uiHost']) {
this.OAuthBaseURL = configHandler.get('region')['uiHost'] || '';
Expand Down Expand Up @@ -132,12 +128,11 @@ class AuthHandler {
*/
async oauth(): Promise<void> {
try {
this.initLog();
await this.initSDK();
await this.createHTTPServer();
await this.openOAuthURL();
} catch (error) {
this.logger.error('OAuth login failed!', error.message);
handleAndLogError(error, { module: 'auth-handler' }, 'OAuth login failed!');
throw error;
}
}
Expand Down
14 changes: 7 additions & 7 deletions packages/contentstack/src/hooks/prerun/auth-guard.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
cliux,
managementSDKClient,
isAuthenticated
isAuthenticated,
log,
handleAndLogError
} from '@contentstack/cli-utilities';
import {LoggerService} from '@contentstack/cli-utilities'
// TBD: run region command if region is not there

export default async function (opts): Promise<void> {
const newLogger = new LoggerService(process.cwd(),'cli-log');
const { context: { plugin: { config: { protectedCommands = {} } = {} } = {}, region = null } = {} } = this.config;
if (opts.Command.id !== 'config:set:region') {
if (!region) {
Expand All @@ -20,21 +20,21 @@ export default async function (opts): Promise<void> {
// Auth guard
if (protectedCommands[opts.Command.id]) {
if (!isAuthenticated()) {
newLogger.error('No auth token found for command.', opts.Command.id);
handleAndLogError(new Error('Authentication required for this command'), { module: 'auth-guard', commandId: opts.Command.id });
cliux.error('Please log in to execute the command');
this.exit();
}
const client = await managementSDKClient({host: region.cma})
try {
const result = await client.getUser();
if (!result) {
newLogger.error('Error in auth validation');
handleAndLogError(new Error('Error in auth validation'), { module: 'auth-guard' });
cliux.error('Please log in to execute the command');
this.exit();
}
newLogger.debug('Logged-in user', result.data);
log.debug('Logged-in user', { module: 'auth-guard', userData: result.data });
} catch (error) {
newLogger.error('Error in auth validation', error);
handleAndLogError(error, { module: 'auth-guard' }, 'Error in auth validation');
cliux.error('Please log in to execute the command');
process.exit();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cliux, configHandler, HttpClient, LoggerService } from '@contentstack/cli-utilities';
import { cliux, configHandler, HttpClient, handleAndLogError } from '@contentstack/cli-utilities';
import * as semver from 'semver';
import { IVersionUpgradeCache, IVersionUpgradeWarningFrequency } from '../../interfaces';

Expand All @@ -8,7 +8,6 @@ const versionUpgradeWarningFrequency: IVersionUpgradeWarningFrequency = {
export default async function (_opts): Promise<void> {
const now = Date.now();
const today = new Date().toISOString().split('T')[0];
const logger: LoggerService = new LoggerService(process.env.CS_CLI_LOG_PATH || process.cwd(), 'cli-log');
let cache: IVersionUpgradeCache = { lastChecked: 0, lastWarnedDate: '', latestVersion: '' };

// if CLI_VERSION is not set or is not the same as the current version, set it
Expand Down Expand Up @@ -36,7 +35,7 @@ export default async function (_opts): Promise<void> {
const latestVersion = (await httpClient.get(`https://registry.npmjs.org/@contentstack/cli/latest`))?.data
?.version;
if (!latestVersion) {
logger.error('Failed to retrieve the latest version from the registry.');
handleAndLogError(new Error('Failed to retrieve the latest version from the registry.'), { module: 'latest-version-warning' });
return;
}
cache.latestVersion = latestVersion;
Expand All @@ -45,7 +44,7 @@ export default async function (_opts): Promise<void> {
// Save updated cache
configHandler.set('versionUpgradeWarningCache', cache);
} catch (error) {
logger.error('Failed to check the latest version', error);
handleAndLogError(error, { module: 'latest-version-warning' }, 'Failed to check the latest version');
return;
}
}
Expand Down
Loading