improvement for the backend api
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user