diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts index 9f62858..99e3efe 100644 --- a/frontend/src/lib/api.ts +++ b/frontend/src/lib/api.ts @@ -1,6 +1,15 @@ -// api.ts const API_BASE_URL = 'http://127.0.0.1:8080'; +// A simple function to retrieve the token from local storage or wherever it is stored +function getAuthToken(): string | null { + return localStorage.getItem('auth_token'); // Assuming the token is stored in localStorage +} + +// A simple function to save the token +function setAuthToken(token: string): void { + localStorage.setItem('auth_token', token); +} + /** * Create a new form. * @param name The name of the form. @@ -8,10 +17,12 @@ const API_BASE_URL = 'http://127.0.0.1:8080'; * @returns The ID of the created form. */ export async function createForm(name: string, fields: unknown): Promise { + const token = getAuthToken(); // Get the token from storage const response = await fetch(`${API_BASE_URL}/forms`, { method: 'POST', headers: { - 'Content-Type': 'application/json' + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}` // Add token to the headers }, body: JSON.stringify({ name, fields }) }); @@ -28,10 +39,12 @@ export async function createForm(name: string, fields: unknown): Promise * @returns An array of forms. */ export async function getForms(): Promise { + const token = getAuthToken(); // Get the token from storage const response = await fetch(`${API_BASE_URL}/forms`, { method: 'GET', headers: { - Accept: 'application/json' + Accept: 'application/json', + Authorization: `Bearer ${token}` // Add token to the headers } }); @@ -49,10 +62,12 @@ export async function getForms(): Promise { * @returns The ID of the created submission. */ export async function submitForm(formId: string, data: unknown): Promise { + const token = getAuthToken(); // Get the token from storage const response = await fetch(`${API_BASE_URL}/forms/${formId}/submissions`, { method: 'POST', headers: { - 'Content-Type': 'application/json' + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}` // Add token to the headers }, body: JSON.stringify(data) }); @@ -70,10 +85,12 @@ export async function submitForm(formId: string, data: unknown): Promise * @returns An array of submissions for the form. */ export async function getSubmissions(formId: string): Promise { + const token = getAuthToken(); // Get the token from storage const response = await fetch(`${API_BASE_URL}/forms/${formId}/submissions`, { method: 'GET', headers: { - Accept: 'application/json' + Accept: 'application/json', + Authorization: `Bearer ${token}` // Add token to the headers } }); @@ -83,3 +100,26 @@ export async function getSubmissions(formId: string): Promise { return await response.json(); } + +/** + * Login and get the authentication token. + * @param username The username. + * @param password The password. + * @returns The authentication token. + */ +export async function login(username: string, password: string): Promise { + const response = await fetch(`${API_BASE_URL}/login`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ username, password }) + }); + + if (!response.ok) { + throw new Error(`Error logging in: ${response.statusText}`); + } + + const { token } = await response.json(); + setAuthToken(token); // Store the token in localStorage +}