Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): extract user email from oauth flow #48

Merged
merged 3 commits into from
Feb 26, 2021
Merged
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
13 changes: 11 additions & 2 deletions packages/cli/src/commands/ui/create/angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Storage} from '../../../lib/oauth/storage';
import {Config} from '../../../lib/config/config';
import {spawnProcess} from '../../../lib/utils/process';
import {buildAnalyticsFailureHook} from '../../../hooks/analytics/analytics';
import {AuthenticatedClient} from '../../../lib/platform/authenticatedClient';

export default class Angular extends Command {
static description =
Expand Down Expand Up @@ -42,6 +43,7 @@ export default class Angular extends Command {
private async addCoveoToProject(applicationName: string, defaults: boolean) {
const cfg = await this.configuration.get();
const storage = await this.storage.get();
const {providerUsername} = await this.getUserInfo();

const cliArgs = [
'add',
Expand All @@ -52,9 +54,8 @@ export default class Angular extends Command {
storage.accessToken!,
'--platform-url',
platformUrl({environment: cfg.environment}),
// TODO: CDX-91 Extract user email from oauth flow
'--user',
'[email protected]',
providerUsername,
];

if (defaults) {
Expand Down Expand Up @@ -84,4 +85,12 @@ export default class Angular extends Command {
private get storage() {
return new Storage();
}

private async getUserInfo() {
const authenticatedClient = new AuthenticatedClient();
const platformClient = await authenticatedClient.getClient();
await platformClient.initialize();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add a method on AuthenticatedClient called getUserInfo that abstract line 91/92 .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Pretty sure, we will use it elsewhere.
I'm creating a JIRA


return await platformClient.user.get();
}
}
13 changes: 11 additions & 2 deletions packages/cli/src/commands/ui/create/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {platformUrl} from '../../../lib/platform/environment';
import {spawnProcess} from '../../../lib/utils/process';
import {Storage} from '../../../lib/oauth/storage';
import AuthenticationRequired from '../../../lib/decorators/authenticationRequired';
import {AuthenticatedClient} from '../../../lib/platform/authenticatedClient';

export default class React extends Command {
static description =
Expand Down Expand Up @@ -49,6 +50,7 @@ export default class React extends Command {
private async setupEnvironmentVariables(name: string) {
const cfg = await this.configuration.get();
const storage = await this.storage.get();
const {providerUsername} = await this.getUserInfo();

return spawnProcess(
'npm',
Expand All @@ -62,9 +64,8 @@ export default class React extends Command {
storage.accessToken!,
'--platformUrl',
platformUrl({environment: cfg.environment}),
// TODO: CDX-91 Extract user email from oauth flow
'--user',
'[email protected]',
providerUsername,
],
{
cwd: name,
Expand All @@ -88,4 +89,12 @@ export default class React extends Command {
private get storage() {
return new Storage();
}

private async getUserInfo() {
const authenticatedClient = new AuthenticatedClient();
const platformClient = await authenticatedClient.getClient();
await platformClient.initialize();

return await platformClient.user.get();
}
}
13 changes: 11 additions & 2 deletions packages/cli/src/commands/ui/create/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '../../../hooks/analytics/analytics';
import {Config} from '../../../lib/config/config';
import AuthenticationRequired from '../../../lib/decorators/authenticationRequired';
import {AuthenticatedClient} from '../../../lib/platform/authenticatedClient';
import {Storage} from '../../../lib/oauth/storage';
import {platformUrl} from '../../../lib/platform/environment';
import {spawnProcess} from '../../../lib/utils/process';
Expand Down Expand Up @@ -68,6 +69,7 @@ export default class Vue extends Command {
private async invokePlugin(applicationName: string) {
const cfg = await this.configuration.get();
const storage = await this.storage.get();
const {providerUsername} = await this.getUserInfo();

const cliArgs = [
'invoke',
Expand All @@ -78,9 +80,8 @@ export default class Vue extends Command {
storage.accessToken!,
'--platformUrl',
platformUrl({environment: cfg.environment}),
// TODO: CDX-91 Extract user email from oauth flow
'--user',
'[email protected]',
providerUsername,
];

return this.runVueCliCommand(cliArgs, {
Expand Down Expand Up @@ -111,4 +112,12 @@ export default class Vue extends Command {
private get storage() {
return new Storage();
}

private async getUserInfo() {
const authenticatedClient = new AuthenticatedClient();
const platformClient = await authenticatedClient.getClient();
await platformClient.initialize();

return await platformClient.user.get();
}
}