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

Oauth URL broken and runtime error with logErrorInstance #460

Merged
merged 4 commits into from
Mar 31, 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
7 changes: 4 additions & 3 deletions packages/cli-lib/oauth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const OAuth2Manager = require('./lib/models/OAuth2Manager');
const { updateAccountConfig, writeConfig } = require('./lib/config');
const { logger, logErrorInstance } = require('./logger');
const { logger } = require('./logger');
const { logErrorInstance } = require('./errorHandlers/standardErrors');
const { AUTH_METHODS } = require('./lib/constants');

const oauthManagers = new Map();
Expand Down Expand Up @@ -35,13 +36,13 @@ const getOauthManager = (accountId, accountConfig) => {
return oauthManagers.get(accountId);
};

const addOauthToAccountConfig = (portalId, oauth) => {
const addOauthToAccountConfig = oauth => {
logger.log('Updating configuration');
try {
updateAccountConfig({
...oauth.toObj(),
authType: AUTH_METHODS.oauth.value,
portalId,
portalId: oauth.accountId,
});
writeConfig();
logger.log('Configuration updated');
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ exports.handler = async options => {
handleExit(deleteEmptyConfigFile);

try {
const { accountId } = await CONFIG_CREATION_FLOWS[authType](env);
const { accountId, name } = await CONFIG_CREATION_FLOWS[authType](env);
const path = getConfigPath();

logger.success(
`The config file "${path}" was created using your personal access key for account ${accountId}.`
`The config file "${path}" was created using your personal access key for account ${name ||
accountId}.`
);

trackAuthAction('init', authType, TRACKING_STATUS.COMPLETE, accountId);
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/lib/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const redirectUri = `http://localhost:${PORT}/oauth-callback`;
const buildAuthUrl = oauthManager => {
return (
`${getHubSpotWebsiteOrigin(oauthManager.env)}/oauth/${
oauthManager.portalId
oauthManager.accountId
}/authorize` +
`?client_id=${encodeURIComponent(oauthManager.clientId)}` + // app's client ID
`&scope=${encodeURIComponent(oauthManager.scopes.join(' '))}` + // scopes being requested by the app
Expand Down Expand Up @@ -79,7 +79,8 @@ const authorize = async oauthManager => {
});
};

const setupOauth = (accountId, accountConfig) => {
const setupOauth = accountConfig => {
const accountId = parseInt(accountConfig.portalId, 10);
const config = getAccountConfig(accountId) || {};
return new OAuth2Manager(
{
Expand All @@ -91,11 +92,10 @@ const setupOauth = (accountId, accountConfig) => {
};

const authenticateWithOauth = async configData => {
const accountId = parseInt(configData.portalId, 10);
const oauthManager = setupOauth(accountId, configData);
const oauthManager = setupOauth(configData);
logger.log('Authorizing');
await authorize(oauthManager);
addOauthToAccountConfig(accountId, oauthManager);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we still be doing the portalId parse on line 94?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Yeah we still need to do that. I moved it into setupOauth because that is where it's needed and it doesn't make sense to do the parsing in authenticateWithOauth anymore.

addOauthToAccountConfig(oauthManager);
};

module.exports = {
Expand Down