feat(api): implement centralized API client and refactor vendor pages

Introduced a new API client in src/lib/api/ to handle requests securely. Refactored vendor pages to use this client. Updated authentication logic in layout and login pages.
This commit is contained in:
2026-02-09 16:25:55 -05:00
parent bd602d58ab
commit a5df1bcacb
14 changed files with 722 additions and 722 deletions

View File

@@ -1,7 +1,8 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { user } from '$lib/states';
import type { LoginRequest } from '$lib/types/types';
import { api } from '$lib/api';
import type { LoginRequest } from '$lib/api';
let username = '';
let password = '';
@@ -17,39 +18,18 @@
isLoading = true;
errorMessage = '';
try {
const response = await fetch('http://localhost:9552/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(loginData)
});
const result = await api.auth.login(loginData);
if (!response.ok) {
const errorData = await response.json();
errorMessage = errorData.error || 'Login failed';
} else {
const data = await response.json();
// Assuming the backend returns a token or some success indicator
// Store the token in localStorage for authentication checks
if (data.token) {
localStorage.setItem('auth_token', data.token);
}
// Store the user object in localStorage
localStorage.setItem('user', JSON.stringify(data.user));
// Update the user store
user.set(data.user);
// Redirect to vendor page after successful login
goto('/vendor');
}
} catch (err) {
errorMessage = 'An error occurred. Please try again.';
} finally {
isLoading = false;
if (result.error) {
errorMessage = result.error;
} else if (result.data) {
// Update the user store
user.set(result.data.user);
// Redirect to vendor page after successful login
goto('/vendor');
}
isLoading = false;
}
</script>