Passing Cookies / Authentication For QStash

Summery: Adding cookies / authentication in my qstash schedule as the API route is protected Note: Everything works fine locally when i use postman for api testing. QStash does hit the endpoint correctly in prod / testing locally Stack: Nextjs: 15v route.ts
import { utapi } from "@/server/uploadthing";
import { waitUntil } from "@vercel/functions";
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server";

export async function POST(req: NextRequest) {
const cookieStore = await cookies();

// Authenticate the request
if (
cookieStore.get("MY_CUSTOM_TOKEN")?.value === process.env.MY_CUSTOM_TOKEN
) {
const fileUploadKeysArr = (await utapi.listFiles()).files.map(
(file) => file.key
);
// If there are no files to delete, return a default message
if (fileUploadKeysArr.length === 0) {
return NextResponse.json(
{ message: "No files to delete" },
{ status: 200 }
);
}

// Deleting all the files
waitUntil(utapi.deleteFiles(fileUploadKeysArr));

return NextResponse.json(
{
message: `Successfully deleted ${fileUploadKeysArr.length} file${
fileUploadKeysArr.length === 1 ? "" : "s"
}`,
},
{ status: 200 }
);
} else {
return NextResponse.json({ message: "Unauthorized" }, { status: 401 });
}
}
import { utapi } from "@/server/uploadthing";
import { waitUntil } from "@vercel/functions";
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server";

export async function POST(req: NextRequest) {
const cookieStore = await cookies();

// Authenticate the request
if (
cookieStore.get("MY_CUSTOM_TOKEN")?.value === process.env.MY_CUSTOM_TOKEN
) {
const fileUploadKeysArr = (await utapi.listFiles()).files.map(
(file) => file.key
);
// If there are no files to delete, return a default message
if (fileUploadKeysArr.length === 0) {
return NextResponse.json(
{ message: "No files to delete" },
{ status: 200 }
);
}

// Deleting all the files
waitUntil(utapi.deleteFiles(fileUploadKeysArr));

return NextResponse.json(
{
message: `Successfully deleted ${fileUploadKeysArr.length} file${
fileUploadKeysArr.length === 1 ? "" : "s"
}`,
},
{ status: 200 }
);
} else {
return NextResponse.json({ message: "Unauthorized" }, { status: 401 });
}
}
Just to rephrase is there any way i can pass on "MY_CUSTOM_TOKEN" from qstash to my server for authentication
No description
Solution:
the solution was using this header
No description
Jump to solution
2 Replies
webdevkaleem
webdevkaleemOP3mo ago
any guidance or response is greatly appreciated thanks in advance
Solution
webdevkaleem
webdevkaleem3mo ago
the solution was using this header
No description

Did you find this page helpful?