Add server ID to Intercom JWT payload (#5769)
This commit is contained in:
@@ -1335,7 +1335,9 @@ async function initializeIntercom() {
|
|||||||
if (!auth.value?.user) return
|
if (!auth.value?.user) return
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const intercomData = await $fetch<{ token: string }>('/api/intercom/messenger-jwt')
|
const intercomData = await $fetch<{ token: string }>('/api/intercom/messenger-jwt', {
|
||||||
|
query: { server_id: serverId },
|
||||||
|
})
|
||||||
|
|
||||||
Intercom({
|
Intercom({
|
||||||
app_id: config.public.intercomAppId,
|
app_id: config.public.intercomAppId,
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ async function getIntercomKeyFromSecretsStore(): Promise<string | undefined> {
|
|||||||
async function signIntercomUserJwt(
|
async function signIntercomUserJwt(
|
||||||
user: { id: string; username: string; email?: string; created: string },
|
user: { id: string; username: string; email?: string; created: string },
|
||||||
secret: string,
|
secret: string,
|
||||||
|
serverId?: string,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const createdAt = Math.floor(new Date(user.created).getTime() / 1000)
|
const createdAt = Math.floor(new Date(user.created).getTime() / 1000)
|
||||||
|
|
||||||
@@ -37,6 +38,10 @@ async function signIntercomUserJwt(
|
|||||||
payload.created_at = createdAt
|
payload.created_at = createdAt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (serverId) {
|
||||||
|
payload.server_id = serverId
|
||||||
|
}
|
||||||
|
|
||||||
return await new SignJWT(payload)
|
return await new SignJWT(payload)
|
||||||
.setProtectedHeader({ alg: 'HS256', typ: 'JWT' })
|
.setProtectedHeader({ alg: 'HS256', typ: 'JWT' })
|
||||||
.setIssuedAt()
|
.setIssuedAt()
|
||||||
@@ -104,7 +109,10 @@ export default defineEventHandler(async (event): Promise<IntercomTokenResponse>
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const token = await signIntercomUserJwt(user, intercomSecret)
|
const query = getQuery(event)
|
||||||
|
const serverId = typeof query.server_id === 'string' ? query.server_id : undefined
|
||||||
|
|
||||||
|
const token = await signIntercomUserJwt(user, intercomSecret, serverId)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
token,
|
token,
|
||||||
|
|||||||
Reference in New Issue
Block a user