Skip to content

Commit 7d12e56

Browse files
feat(training): update download training certificate action to forward session cookies for authentication (#2300)
Co-authored-by: Tofik Hasanov <annexcies@gmail.com>
1 parent 3f8cb4b commit 7d12e56

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

apps/app/src/app/(app)/[orgId]/people/[employeeId]/actions/download-training-certificate.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { authActionClient } from '@/actions/safe-action';
44
import { env } from '@/env.mjs';
5+
import { headers } from 'next/headers';
56
import { z } from 'zod';
67

78
const downloadCertificateSchema = z.object({
@@ -32,23 +33,26 @@ export const downloadTrainingCertificate = authActionClient
3233
);
3334
}
3435

35-
// Call the API to generate the certificate
3636
const apiUrl =
3737
env.NEXT_PUBLIC_API_URL ||
3838
process.env.API_BASE_URL ||
3939
'http://localhost:3333';
4040

41-
const internalToken = env.INTERNAL_API_TOKEN;
42-
if (!internalToken) {
43-
throw new Error('INTERNAL_API_TOKEN not configured');
41+
// Forward session cookies for authentication
42+
const headerStore = await headers();
43+
const cookieHeader = headerStore.get('cookie');
44+
45+
const requestHeaders: Record<string, string> = {
46+
'Content-Type': 'application/json',
47+
};
48+
49+
if (cookieHeader) {
50+
requestHeaders['Cookie'] = cookieHeader;
4451
}
4552

4653
const response = await fetch(`${apiUrl}/v1/training/generate-certificate`, {
4754
method: 'POST',
48-
headers: {
49-
'Content-Type': 'application/json',
50-
'x-internal-token': internalToken,
51-
},
55+
headers: requestHeaders,
5256
body: JSON.stringify({
5357
memberId,
5458
organizationId,

0 commit comments

Comments
 (0)