authtoken and stuff
This commit is contained in:
parent
b110821483
commit
227692b68e
@ -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<any> {
|
||||
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<string> {
|
||||
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<Form[]> {
|
||||
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<Submission[]> {
|
||||
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<string>
|
||||
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<Submission[]> {
|
||||
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<string>
|
||||
}
|
||||
|
||||
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<string> {
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data.message; // Assuming the response contains a success message
|
||||
return data.message;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user