Skip to content

Commit

Permalink
feat(cli): use oauth device grant by default for cli auth
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewvolk committed Feb 16, 2024
1 parent 650256a commit 4adf11d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 40 deletions.
2 changes: 0 additions & 2 deletions packages/create-catalyst/src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ export const create = async (options: CreateCommandOptions) => {
canCreateChannel = false;
} else if (channels.length >= limits.total_including_inactive) {
canCreateChannel = false;
} else {
console.log(`${limits.active - activeChannels.length} active channels remaining.`);
}

if (!canCreateChannel) {
Expand Down
7 changes: 2 additions & 5 deletions packages/create-catalyst/src/utils/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface HttpsConfig {
}

export class Https {
DEVICE_OAUTH_CLIENT_ID = 'o094w49fnipwgbo0h9772pc2q6pw1p7';
DEVICE_OAUTH_CLIENT_ID = 'acse0vvawm9r1n0evag4b8e1ea1fo90';

bigCommerceApiUrl: string;
bigCommerceAuthUrl: string;
Expand Down Expand Up @@ -118,10 +118,7 @@ export class Https {
});

if (response.status !== 200) {
console.error(
chalk.red(`\nPOST /device/token failed: ${response.status} ${response.statusText}\n`),
);
process.exit(1);
throw new Error('Device code not yet verified');
}

const DeviceCodeSuccessSchema = z.object({
Expand Down
36 changes: 3 additions & 33 deletions packages/create-catalyst/src/utils/login.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/* eslint-disable no-await-in-loop */

import { confirm, input } from '@inquirer/prompts';
import { confirm } from '@inquirer/prompts';
import chalk from 'chalk';

import { Https } from './https';
import { spinner } from './spinner';

const DEVICE_OAUTH_ENABLED = process.env.DEVICE_OAUTH_ENABLED ?? false;

const poll = async (auth: Https, deviceCode: string, interval: number, expiresIn: number) => {
const intervalMs = interval * 1000;
const expiresAtMs = expiresIn * 1000;
Expand Down Expand Up @@ -44,34 +42,6 @@ export const login = async (
return { storeHash, accessToken };
}

if (!DEVICE_OAUTH_ENABLED) {
console.log(
[
`\nPlease create an access token at ${chalk.cyan(
`${bigCommerceAuthUrl}/deep-links/settings/api-accounts/create`,
)} with the following scopes:`,
` - Content: ${chalk.yellow('modify')}`,
` - Information & settings: ${chalk.yellow('modify')}`,
` - Products: ${chalk.yellow('modify')}`,
` - Carts: ${chalk.yellow('read-only')}`,
` - Sites & routes: ${chalk.yellow('modify')}`,
` - Channel settings: ${chalk.yellow('modify')}`,
` - Storefront API customer impersonation tokens: ${chalk.yellow('manage')}\n`,
].join('\n'),
);

return {
storeHash: await input({
message: 'Enter your store hash',
validate: (i) => (i.length > 0 ? true : 'Store hash is required'),
}),
accessToken: await input({
message: 'Enter your access token',
validate: (i) => (i.length > 0 ? true : 'Access token is required'),
}),
};
}

const auth = new Https({ bigCommerceAuthUrl });

const deviceCode = await auth.getDeviceCode();
Expand All @@ -89,8 +59,8 @@ export const login = async (
poll(auth, deviceCode.device_code, deviceCode.interval, deviceCode.expires_in),
{
text: 'Waiting for device code to be authorized',
successText: 'Device code authorized',
failText: 'Device code expired',
successText: 'Device code authorized\n',
failText: 'Device code expired\n',
},
);

Expand Down

0 comments on commit 4adf11d

Please sign in to comment.