Add authentication to the request header
This commit is contained in:
@ -27,6 +27,7 @@ const createHttpTaskWithToken = async ({
|
||||
payload,
|
||||
priority = 'high',
|
||||
scheduleTime,
|
||||
requestHeaders,
|
||||
}: {
|
||||
project: string
|
||||
queue?: string
|
||||
@ -36,6 +37,7 @@ const createHttpTaskWithToken = async ({
|
||||
payload: unknown
|
||||
priority?: 'low' | 'high'
|
||||
scheduleTime?: number
|
||||
requestHeaders?: Record<string, string>
|
||||
}): Promise<
|
||||
[
|
||||
protos.google.cloud.tasks.v2.ITask,
|
||||
@ -71,6 +73,7 @@ const createHttpTaskWithToken = async ({
|
||||
url: taskHandlerUrl,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...requestHeaders,
|
||||
},
|
||||
body,
|
||||
...(serviceAccountEmail
|
||||
@ -409,7 +412,8 @@ export const enqueueTextToSpeech = async ({
|
||||
export const enqueueRecommendation = async (
|
||||
userId: string,
|
||||
pageId: string,
|
||||
recommendation: Recommendation
|
||||
recommendation: Recommendation,
|
||||
authToken: string
|
||||
): Promise<string> => {
|
||||
const { GOOGLE_CLOUD_PROJECT } = process.env
|
||||
const payload = {
|
||||
@ -418,15 +422,29 @@ export const enqueueRecommendation = async (
|
||||
recommendation,
|
||||
}
|
||||
|
||||
const headers = {
|
||||
Authorization: authToken,
|
||||
}
|
||||
// If there is no Google Cloud Project Id exposed, it means that we are in local environment
|
||||
if (env.dev.isLocal || !GOOGLE_CLOUD_PROJECT) {
|
||||
return nanoid()
|
||||
// Calling the handler function directly.
|
||||
setTimeout(() => {
|
||||
axios
|
||||
.post(env.queue.recommendationTaskHandlerUrl, payload, {
|
||||
headers,
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.error(error)
|
||||
})
|
||||
}, 0)
|
||||
return ''
|
||||
}
|
||||
|
||||
const createdTasks = await createHttpTaskWithToken({
|
||||
project: GOOGLE_CLOUD_PROJECT,
|
||||
payload,
|
||||
taskHandlerUrl: env.queue.recommendationTaskHandlerUrl,
|
||||
requestHeaders: headers,
|
||||
})
|
||||
|
||||
if (!createdTasks || !createdTasks[0].name) {
|
||||
|
||||
Reference in New Issue
Block a user