From 227692b68ea7bfcd5209109ea9b6e210afe75dc8 Mon Sep 17 00:00:00 2001 From: Mohamad Date: Mon, 30 Dec 2024 14:53:00 +0100 Subject: [PATCH] authtoken and stuff --- frontend/src/lib/api.ts | 93 ++++++++++++++++++++--------------------- 1 file changed, 46 insertions(+), 47 deletions(-) diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts index e489d64..9e7dbf3 100644 --- a/frontend/src/lib/api.ts +++ b/frontend/src/lib/api.ts @@ -3,48 +3,66 @@ import type { Form, Submission, LoginCredentials } from './types'; const API_BASE_URL = 'http://127.0.0.1:8080'; /** - * Create a new form. + * Helper to make authenticated requests. + * @param endpoint The API endpoint (relative to base URL). + * @param options Fetch options such as method, headers, and body. + * @returns The JSON-parsed response. + */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +async function authenticatedRequest(endpoint: string, options: RequestInit): Promise { + const token = localStorage.getItem('authToken'); // Replace with a secure token storage solution if needed + if (!token) { + throw new Error('Authentication token is missing. Please log in.'); + } + + const response = await fetch(`${API_BASE_URL}${endpoint}`, { + ...options, + headers: { + ...options.headers, + Authorization: `Bearer ${token}`, // Include the token in the Authorization header + 'Content-Type': 'application/json' + } + }); + + if (!response.ok) { + throw new Error(`Error: ${response.statusText}`); + } + + return response.json(); +} + +/** + * Create a new form (authenticated). * @param name The name of the form. * @param fields The fields of the form in JSON format. * @returns The ID of the created form. */ export async function createForm(name: string, fields: unknown): Promise { - const response = await fetch(`${API_BASE_URL}/forms`, { + return await authenticatedRequest('/forms', { method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, body: JSON.stringify({ name, fields }) }); - - if (!response.ok) { - throw new Error(`Error creating form: ${response.statusText}`); - } - - return await response.json(); } /** - * Get all forms. + * Get all forms (authenticated). * @returns An array of forms. */ export async function getForms(): Promise { - const response = await fetch(`${API_BASE_URL}/forms`, { - method: 'GET', - headers: { - Accept: 'application/json' - } - }); - - if (!response.ok) { - throw new Error(`Error fetching forms: ${response.statusText}`); - } - - return await response.json(); + return await authenticatedRequest('/forms', { method: 'GET' }); } /** - * Submit a form. + * Get all submissions for a specific form (authenticated). + * @param formId The ID of the form. + * @returns An array of submissions for the form. + */ +export async function getSubmissions(formId: string): Promise { + return await authenticatedRequest(`/forms/${formId}/submissions`, { method: 'GET' }); +} + +/** + * Submit a form (unauthenticated). * @param formId The ID of the form to submit. * @param data The submission data in JSON format. * @returns The ID of the created submission. @@ -65,26 +83,6 @@ export async function submitForm(formId: string, data: unknown): Promise return await response.json(); } -/** - * Get all submissions for a specific form. - * @param formId The ID of the form. - * @returns An array of submissions for the form. - */ -export async function getSubmissions(formId: string): Promise { - const response = await fetch(`${API_BASE_URL}/forms/${formId}/submissions`, { - method: 'GET', - headers: { - Accept: 'application/json' - } - }); - - if (!response.ok) { - throw new Error(`Error fetching submissions: ${response.statusText}`); - } - - return await response.json(); -} - /** * Admin login to get a token. * @param credentials The login credentials (username and password). @@ -104,7 +102,8 @@ export async function adminLogin(credentials: LoginCredentials): Promise } const data = await response.json(); - return data.token; // Assuming the response contains the token + localStorage.setItem('authToken', data.token); // Store token locally + return data.token; } /** @@ -126,5 +125,5 @@ export async function createAdmin(user: LoginCredentials): Promise { } const data = await response.json(); - return data.message; // Assuming the response contains a success message + return data.message; }