Skip to content

Commit 62a51ce

Browse files
committed
added a way to tell the client we need to run through the initial oauth flow
1 parent 54cd5b0 commit 62a51ce

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/app.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { saveMapping, getMappings, deleteMapping } from "./mappings";
1919
import { PORT, getCustomerId } from "./utils/utils";
2020
import { Mapping, Properties } from "@prisma/client";
2121
import handleError from "./utils/error";
22+
import { PropertyUpdate } from '@hubspot/api-client/lib/codegen/crm/properties';
2223

2324
const app: Application = express();
2425
app.use(express.json());
@@ -85,6 +86,10 @@ app.get(
8586
try {
8687
const customerId: string = getCustomerId();
8788
const properties = await getHubSpotProperties(customerId, false);
89+
90+
if(!properties){
91+
res.send(authUrl)
92+
}
8893
res.send(properties);
8994
} catch (error) {
9095
handleError(error, "There was an issue getting Hubspot properties ");

src/auth.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ const exchangeForTokens = async (
101101
grant_type,
102102
refresh_token,
103103
} = exchangeProof;
104-
105104
try {
106105
const tokenResponse = await hubspotClient.oauth.tokensApi.create(
107106
grant_type,
@@ -165,9 +164,17 @@ const getAccessToken = async (customerId: string): Promise => {
165164
customerId,
166165
},
167166
})) as Authorization;
168-
if (currentCreds?.expiresAt && currentCreds?.expiresAt > new Date()) {
167+
console.log('Current Creds info')
168+
console.log(currentCreds)
169+
console.log(typeof currentCreds)
170+
console.log('===============')
171+
if(!currentCreds){
172+
return "missing" // considering throwing an error here intead of a string that you have to know what it means
173+
}
174+
else if (currentCreds?.expiresAt && currentCreds?.expiresAt > new Date()) {
169175
return currentCreds?.accessToken;
170-
} else {
176+
}
177+
else {
171178
const updatedCreds = await exchangeForTokens({
172179
...EXCHANGE_CONSTANTS,
173180
grant_type: "refresh_token",

src/properties.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,14 @@ export const getHubSpotProperties = async (
260260

261261
const accessToken: string | void = await getAccessToken(customerId);
262262
console.log(accessToken);
263+
if(accessToken == 'missing'){
264+
return undefined
265+
}
266+
267+
if (accessToken){ hubspotClient.setAccessToken(accessToken)}
268+
269+
263270

264-
if (accessToken) hubspotClient.setAccessToken(accessToken);
265271
// add DB call to check if we've looked in the last 5 minutes
266272
// https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#updatedat
267273
const cacheResults = await checkPropertiesCache(customerId);

0 commit comments

Comments
 (0)