aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/src/utils/api.ts
diff options
context:
space:
mode:
authorPinapelz <yukais@pinapelz.com>2025-06-29 01:28:39 -0700
committerPinapelz <yukais@pinapelz.com>2025-06-29 01:28:39 -0700
commitff37cca46430ed714015647469f88ce06781457a (patch)
tree63f406a3c7fb463fed7f34efc8d04d58fe96e0cb /frontend/src/utils/api.ts
parente4fa1e69e7ebfb627c7198fd1a9881e9327ec4d4 (diff)
scaffold register,login,and auth endpoints
Diffstat (limited to 'frontend/src/utils/api.ts')
-rw-r--r--frontend/src/utils/api.ts161
1 files changed, 161 insertions, 0 deletions
diff --git a/frontend/src/utils/api.ts b/frontend/src/utils/api.ts
new file mode 100644
index 0000000..528c170
--- /dev/null
+++ b/frontend/src/utils/api.ts
@@ -0,0 +1,161 @@
+const API_BASE_URL = import.meta.env.VITE_API_URL;
+
+// Auth API functions
+export const authApi = {
+ async login(credentials: { username: string; password: string }) {
+ try {
+ const response = await fetch(`${API_BASE_URL}/authenticate`, {
+ method: 'POST',
+ credentials: 'include',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(credentials),
+ });
+
+ const data = await response.json();
+
+ if (!response.ok) {
+ return { error: data.error || `HTTP ${response.status}` };
+ }
+
+ return { data };
+ } catch (error) {
+ console.error('Login failed:', error);
+ return { error: 'Network error. Please check your connection.' };
+ }
+ },
+
+ async register(userData: {
+ username: string;
+ email: string;
+ password: string;
+ }) {
+ try {
+ const response = await fetch(`${API_BASE_URL}/register`, {
+ method: 'POST',
+ credentials: 'include',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(userData),
+ });
+
+ const data = await response.json();
+
+ if (!response.ok) {
+ return { error: data.error || `HTTP ${response.status}` };
+ }
+
+ return { data };
+ } catch (error) {
+ console.error('Registration failed:', error);
+ return { error: 'Network error. Please check your connection.' };
+ }
+ },
+
+ async logout() {
+ try {
+ const response = await fetch(`${API_BASE_URL}/logout`, {
+ method: 'POST',
+ credentials: 'include',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ });
+
+ const data = await response.json();
+
+ if (!response.ok) {
+ return { error: data.error || `HTTP ${response.status}` };
+ }
+
+ return { data };
+ } catch (error) {
+ console.error('Logout failed:', error);
+ return { error: 'Network error. Please check your connection.' };
+ }
+ },
+
+ async getSession() {
+ try {
+ const response = await fetch(`${API_BASE_URL}/session`, {
+ method: 'GET',
+ credentials: 'include',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ });
+
+ const data = await response.json();
+
+ if (!response.ok) {
+ return { error: data.error || `HTTP ${response.status}` };
+ }
+
+ return { data };
+ } catch (error) {
+ console.error('Session check failed:', error);
+ return { error: 'Network error. Please check your connection.' };
+ }
+ },
+
+ async getCurrentUser() {
+ try {
+ const response = await fetch(`${API_BASE_URL}/me`, {
+ method: 'GET',
+ credentials: 'include',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ });
+
+ const data = await response.json();
+
+ if (!response.ok) {
+ return { error: data.error || `HTTP ${response.status}` };
+ }
+
+ return { data };
+ } catch (error) {
+ console.error('Get current user failed:', error);
+ return { error: 'Network error. Please check your connection.' };
+ }
+ },
+};
+
+export const infoApi = {
+ async getUsers() {
+ try {
+ const response = await fetch(`${API_BASE_URL}/users`, {
+ method: 'GET',
+ credentials: 'include',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ });
+
+ const data = await response.json();
+
+ if (!response.ok) {
+ return { error: data.error || `HTTP ${response.status}` };
+ }
+
+ return { data };
+ } catch (error) {
+ console.error('Get users failed:', error);
+ return { error: 'Network error. Please check your connection.' };
+ }
+ },
+};
+
+export interface User {
+ id: number;
+ username: string;
+ email: string;
+}
+
+export interface SessionResponse {
+ authenticated: boolean;
+ user?: User;
+}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage