Skip to content
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
2 changes: 1 addition & 1 deletion apps/frontend/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export default defineNuxtConfig({
rateLimitKey: process.env.RATE_LIMIT_IGNORE_KEY ?? globalThis.RATE_LIMIT_IGNORE_KEY,
pyroBaseUrl: process.env.PYRO_BASE_URL,
intercomIdentitySecret:
process.env.INTERCOM_IDENTITY_SECRET ||
process.env.INTERCOM_IDENTITY_SECRET ??
// @ts-ignore
globalThis.INTERCOM_IDENTITY_SECRET,
public: {
Expand Down
19 changes: 16 additions & 3 deletions apps/frontend/src/server/routes/api/intercom/messenger-jwt.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ type IntercomTokenResponse = {
token: string
}

async function getIntercomKeyFromSecretsStore(): Promise<string | undefined> {
try {
const mod = 'cloudflare:workers'
const { env } = await import(/* @vite-ignore */ mod)
return await env.INTERCOM_IDENTITY_SECRET?.get()
} catch {
// Not running in Cloudflare Workers environment
return undefined
}
}

async function signIntercomUserJwt(
user: { id: string; username: string; email?: string; created: string },
secret: string,
Expand Down Expand Up @@ -51,8 +62,10 @@ export default defineEventHandler(async (event): Promise<IntercomTokenResponse>

setHeader(event, 'cache-control', 'private, no-store, max-age=0')

const config = useRuntimeConfig(event)
if (!config.intercomIdentitySecret) {
const intercomSecret =
useRuntimeConfig(event).intercomIdentitySecret ?? (await getIntercomKeyFromSecretsStore())

if (!intercomSecret) {
throw createError({
statusCode: 500,
message: 'Intercom identity secret is not configured',
Expand Down Expand Up @@ -91,7 +104,7 @@ export default defineEventHandler(async (event): Promise<IntercomTokenResponse>
})
}

const token = await signIntercomUserJwt(user, config.intercomIdentitySecret)
const token = await signIntercomUserJwt(user, intercomSecret)

return {
token,
Expand Down
Loading