Skip to content

Scheduling infrastructure for absolutely everyone.

feat: add user hash for intercom (#8263)

Nafees Nazik8a2ab68e9a30
diff --git a/.env.example b/.env.example
index 0c369e0dd8f..4ddb81f5eb0 100644
--- a/.env.example
+++ b/.env.example
@@ -79,6 +79,9 @@ CALENDSO_ENCRYPTION_KEY=
# Intercom Config
NEXT_PUBLIC_INTERCOM_APP_ID=
+# Secret to enable Intercom Identity Verification
+INTERCOM_SECRET=
+
# Zendesk Config
NEXT_PUBLIC_ZENDESK_KEY=
diff --git a/apps/web/pages/api/intercom-hash.ts b/apps/web/pages/api/intercom-hash.ts
new file mode 100644
index 00000000000..54f2419331c
--- /dev/null
+++ b/apps/web/pages/api/intercom-hash.ts
@@ -0,0 +1,28 @@
+import type { NextApiRequest, NextApiResponse } from "next";
+import crypto from "node:crypto";
+
+import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
+import { defaultHandler } from "@calcom/lib/server";
+
+async function handler(req: NextApiRequest, res: NextApiResponse) {
+ const session = await getServerSession({ req, res });
+ const secret = process.env.INTERCOM_SECRET;
+
+ if (!session) {
+ return res.status(401).json({ message: "user not authenticated" });
+ }
+
+ if (!secret) {
+ return res.status(400).json({ message: "Intercom Identity Verification secret not set" });
+ }
+
+ const hmac = crypto.createHmac("sha256", secret);
+ hmac.update(String(session.user.id));
+ const hash = hmac.digest("hex");
+
+ return res.status(200).json({ hash });
+}
+
+export default defaultHandler({
+ GET: Promise.resolve({ default: handler }),
+});
diff --git a/apps/web/pages/settings/billing/index.tsx b/apps/web/pages/settings/billing/index.tsx
index 00cf1c14b99..142c6df4fb0 100644
--- a/apps/web/pages/settings/billing/index.tsx
+++ b/apps/web/pages/settings/billing/index.tsx
@@ -39,8 +39,8 @@ const BillingView = () => {
const returnTo = router.asPath;
const billingHref = `/api/integrations/stripepayment/portal?returnTo=${WEBAPP_URL}${returnTo}`;
- const onContactSupportClick = () => {
- open();
+ const onContactSupportClick = async () => {
+ await open();
};
return (
diff --git a/packages/features/ee/support/components/HelpMenuItem.tsx b/packages/features/ee/support/components/HelpMenuItem.tsx
index 7bcdf66e4c3..98b2b378aaa 100644
--- a/packages/features/ee/support/components/HelpMenuItem.tsx
+++ b/packages/features/ee/support/components/HelpMenuItem.tsx
@@ -187,12 +187,12 @@ export default function HelpMenuItem({ onHelpItemSelect }: HelpMenuItemProps) {
<p className="">{t("specific_issue")}</p>
<button
className="hover:text-emphasis text-defualt font-medium underline"
- onClick={() => {
+ onClick={async () => {
setActive(true);
if (isFreshChatEnabled) {
setFreshChat(true);
} else if (isInterComEnabled) {
- open();
+ await open();
} else {
loadChat({ open: true });
}
diff --git a/packages/features/ee/support/lib/intercom/IntercomMenuItem.tsx b/packages/features/ee/support/lib/intercom/IntercomMenuItem.tsx
index feb45efaad3..ee8348fd688 100644
--- a/packages/features/ee/support/lib/intercom/IntercomMenuItem.tsx
+++ b/packages/features/ee/support/lib/intercom/IntercomMenuItem.tsx
@@ -15,8 +15,8 @@ export default function IntercomMenuItem(props: IntercomMenuItemProps) {
return (
<button
- onClick={() => {
- open();
+ onClick={async () => {
+ await open();
onHelpItemSelect();
}}
className="hover:bg-subtle hover:text-emphasis text-default flex w-full px-5 py-2 pr-4 text-sm font-medium">
diff --git a/packages/features/ee/support/lib/intercom/useIntercom.ts b/packages/features/ee/support/lib/intercom/useIntercom.ts
index 87d8e3b84c7..bbf6391f373 100644
--- a/packages/features/ee/support/lib/intercom/useIntercom.ts
+++ b/packages/features/ee/support/lib/intercom/useIntercom.ts
@@ -25,12 +25,21 @@ export const useIntercom = () => {
const { hasPaidPlan } = useHasPaidPlan();
const { hasTeamPlan } = useHasTeamPlan();
- const open = () => {
+ const open = async () => {
+ let userHash;
+
+ const req = await fetch(`/api/intercom-hash`);
+ const res = await req.json();
+ if (res?.hash) {
+ userHash = res.hash;
+ }
+
hookData.boot({
name: data?.name ?? "",
email: data?.email,
userId: String(data?.id),
createdAt: String(dayjs(data?.createdDate).unix()),
+ ...(userHash && { userHash }),
customAttributes: {
//keys should be snake cased
user_name: data?.username,
diff --git a/turbo.json b/turbo.json
index 9d2009e5308..2ab5a86df02 100644
--- a/turbo.json
+++ b/turbo.json
@@ -277,6 +277,7 @@
"ZOHOCRM_CLIENT_ID",
"ZOHOCRM_CLIENT_SECRET",
"ZOOM_CLIENT_ID",
- "ZOOM_CLIENT_SECRET"
+ "ZOOM_CLIENT_SECRET",
+ "INTERCOM_SECRET"
]
}