some fixes and refactors
This commit is contained in:
parent
ead8a566d0
commit
75cfc6c928
16 changed files with 381 additions and 425 deletions
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
import { createContext, useContext, useState, useEffect, useCallback, ReactNode } from "react";
|
||||
|
||||
import { API_URL } from "./config";
|
||||
import { api, ApiError } from "./api";
|
||||
|
||||
// Permission constants matching backend
|
||||
export const Permission = {
|
||||
|
|
@ -43,13 +43,8 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
|||
|
||||
const checkAuth = async () => {
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/api/auth/me`, {
|
||||
credentials: "include",
|
||||
});
|
||||
if (res.ok) {
|
||||
const userData = await res.json();
|
||||
setUser(userData);
|
||||
}
|
||||
const userData = await api.get<User>("/api/auth/me");
|
||||
setUser(userData);
|
||||
} catch {
|
||||
// Not authenticated
|
||||
} finally {
|
||||
|
|
@ -58,44 +53,37 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
|||
};
|
||||
|
||||
const login = async (email: string, password: string) => {
|
||||
const res = await fetch(`${API_URL}/api/auth/login`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
credentials: "include",
|
||||
body: JSON.stringify({ email, password }),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const error = await res.json();
|
||||
throw new Error(error.detail || "Login failed");
|
||||
try {
|
||||
const userData = await api.post<User>("/api/auth/login", { email, password });
|
||||
setUser(userData);
|
||||
} catch (err) {
|
||||
if (err instanceof ApiError) {
|
||||
const data = err.data as { detail?: string };
|
||||
throw new Error(data?.detail || "Login failed");
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
const userData = await res.json();
|
||||
setUser(userData);
|
||||
};
|
||||
|
||||
const register = async (email: string, password: string) => {
|
||||
const res = await fetch(`${API_URL}/api/auth/register`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
credentials: "include",
|
||||
body: JSON.stringify({ email, password }),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const error = await res.json();
|
||||
throw new Error(error.detail || "Registration failed");
|
||||
try {
|
||||
const userData = await api.post<User>("/api/auth/register", { email, password });
|
||||
setUser(userData);
|
||||
} catch (err) {
|
||||
if (err instanceof ApiError) {
|
||||
const data = err.data as { detail?: string };
|
||||
throw new Error(data?.detail || "Registration failed");
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
const userData = await res.json();
|
||||
setUser(userData);
|
||||
};
|
||||
|
||||
const logout = async () => {
|
||||
await fetch(`${API_URL}/api/auth/logout`, {
|
||||
method: "POST",
|
||||
credentials: "include",
|
||||
});
|
||||
try {
|
||||
await api.post("/api/auth/logout");
|
||||
} catch {
|
||||
// Ignore errors on logout
|
||||
}
|
||||
setUser(null);
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue