improvement for the backend api

This commit is contained in:
ivannoskov
2025-11-19 12:56:04 +03:00
parent b0a9934220
commit d4672ea32b
40 changed files with 2383 additions and 3313 deletions

View File

@@ -13,7 +13,7 @@ import React, {
} from "react";
import { useRouter } from "next/navigation";
import { authApi } from "@/lib/api";
import { USE_MOCKS } from "@/lib/utils/constants";
import { USE_MOCKS, STORAGE_KEYS } from "@/lib/utils/constants";
import type { User } from "@/lib/types/api";
interface AuthContextType {
@@ -85,13 +85,28 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
setLoading(true);
setError(null);
const response = await authApi.complete(token);
setUser(response.user);
// Exchange token for JWT
await authApi.complete(token);
// Fetch user data with the JWT token
if (!USE_MOCKS) {
const userData = await authApi.me();
setUser(userData);
// Save user to storage
if (typeof window !== "undefined") {
localStorage.setItem(STORAGE_KEYS.USER, JSON.stringify(userData));
}
} else {
// For mocks, get user from mock data
const userData = authApi.getUserFromStorage();
setUser(userData);
}
// Redirect to home
router.push("/");
} catch (err: any) {
const errorMessage = err?.error?.message || "Ошибка авторизации";
const errorMessage = err?.detail || err?.error?.message || "Ошибка авторизации";
setError(errorMessage);
throw err;
} finally {