added basic auth
This commit is contained in:
parent
793e136087
commit
1b21ec4f32
@ -1,6 +1,15 @@
|
|||||||
// api.ts
|
|
||||||
const API_BASE_URL = 'http://127.0.0.1:8080';
|
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.
|
* Create a new form.
|
||||||
* @param name The name of the 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.
|
* @returns The ID of the created form.
|
||||||
*/
|
*/
|
||||||
export async function createForm(name: string, fields: unknown): Promise<string> {
|
export async function createForm(name: string, fields: unknown): Promise<string> {
|
||||||
|
const token = getAuthToken(); // Get the token from storage
|
||||||
const response = await fetch(`${API_BASE_URL}/forms`, {
|
const response = await fetch(`${API_BASE_URL}/forms`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: `Bearer ${token}` // Add token to the headers
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ name, fields })
|
body: JSON.stringify({ name, fields })
|
||||||
});
|
});
|
||||||
@ -28,10 +39,12 @@ export async function createForm(name: string, fields: unknown): Promise<string>
|
|||||||
* @returns An array of forms.
|
* @returns An array of forms.
|
||||||
*/
|
*/
|
||||||
export async function getForms(): Promise<unknown[]> {
|
export async function getForms(): Promise<unknown[]> {
|
||||||
|
const token = getAuthToken(); // Get the token from storage
|
||||||
const response = await fetch(`${API_BASE_URL}/forms`, {
|
const response = await fetch(`${API_BASE_URL}/forms`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
Accept: 'application/json'
|
Accept: 'application/json',
|
||||||
|
Authorization: `Bearer ${token}` // Add token to the headers
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -49,10 +62,12 @@ export async function getForms(): Promise<unknown[]> {
|
|||||||
* @returns The ID of the created submission.
|
* @returns The ID of the created submission.
|
||||||
*/
|
*/
|
||||||
export async function submitForm(formId: string, data: unknown): Promise<string> {
|
export async function submitForm(formId: string, data: unknown): Promise<string> {
|
||||||
|
const token = getAuthToken(); // Get the token from storage
|
||||||
const response = await fetch(`${API_BASE_URL}/forms/${formId}/submissions`, {
|
const response = await fetch(`${API_BASE_URL}/forms/${formId}/submissions`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: `Bearer ${token}` // Add token to the headers
|
||||||
},
|
},
|
||||||
body: JSON.stringify(data)
|
body: JSON.stringify(data)
|
||||||
});
|
});
|
||||||
@ -70,10 +85,12 @@ export async function submitForm(formId: string, data: unknown): Promise<string>
|
|||||||
* @returns An array of submissions for the form.
|
* @returns An array of submissions for the form.
|
||||||
*/
|
*/
|
||||||
export async function getSubmissions(formId: string): Promise<unknown[]> {
|
export async function getSubmissions(formId: string): Promise<unknown[]> {
|
||||||
|
const token = getAuthToken(); // Get the token from storage
|
||||||
const response = await fetch(`${API_BASE_URL}/forms/${formId}/submissions`, {
|
const response = await fetch(`${API_BASE_URL}/forms/${formId}/submissions`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
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<unknown[]> {
|
|||||||
|
|
||||||
return await response.json();
|
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<void> {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user