mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-24 23:41:10 +02:00
Translations: - Add complete Latvian translations (lv.json) - Remove Chinese translations (zh.json) - Update i18n routing to support en/lv locales - Update LanguageSwitcher component for Latvian - Update LocaleContext to use Latvian messages PR Review Improvements: - Create shared handleSessionExpired() utility in auth-utils.ts - Refactor use-user.ts, use-chats.ts, use-search-space.ts to use shared utility - Add session_expired error message to auth-errors.ts - Consistent redirect to /login?error=session_expired
59 lines
1.3 KiB
TypeScript
59 lines
1.3 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { toast } from "sonner";
|
|
import { handleSessionExpired } from "@/lib/auth-utils";
|
|
|
|
interface User {
|
|
id: string;
|
|
email: string;
|
|
is_active: boolean;
|
|
is_superuser: boolean;
|
|
is_verified: boolean;
|
|
pages_limit: number;
|
|
pages_used: number;
|
|
}
|
|
|
|
export function useUser() {
|
|
const [user, setUser] = useState<User | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
const fetchUser = async () => {
|
|
try {
|
|
// Only run on client-side
|
|
if (typeof window === "undefined") return;
|
|
|
|
setLoading(true);
|
|
const response = await fetch(`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/users/me`, {
|
|
headers: {
|
|
Authorization: `Bearer ${localStorage.getItem("surfsense_bearer_token")}`,
|
|
},
|
|
method: "GET",
|
|
});
|
|
|
|
if (response.status === 401) {
|
|
handleSessionExpired();
|
|
}
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to fetch user: ${response.status}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
setUser(data);
|
|
setError(null);
|
|
} catch (err: any) {
|
|
setError(err.message || "Failed to fetch user");
|
|
console.error("Error fetching user:", err);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
fetchUser();
|
|
}, []);
|
|
|
|
return { user, loading, error };
|
|
}
|