Add new frontend - fix 15
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
// frontend/src/contexts/AuthContext.tsx - FIXED VERSION
|
||||
import React, { createContext, useContext, useEffect, useState, useCallback } from 'react';
|
||||
// FIXED: Import authService directly, not through the index
|
||||
import { authService } from '../api/services/authService';
|
||||
import { tokenManager } from '../api/auth/tokenManager';
|
||||
import {
|
||||
UserProfile,
|
||||
RegisterRequest,
|
||||
api
|
||||
} from '../api/services';
|
||||
} from '../api/types/api';
|
||||
|
||||
interface AuthContextType {
|
||||
user: UserProfile | null;
|
||||
isAuthenticated: boolean;
|
||||
isLoading: boolean;
|
||||
login: (email: string, password: string) => Promise<void>;
|
||||
register: (data: RegisterRequest) => Promise<void>; // FIXED: Use RegisterRequest
|
||||
register: (data: RegisterRequest) => Promise<void>;
|
||||
logout: () => Promise<void>;
|
||||
updateProfile: (updates: Partial<UserProfile>) => Promise<void>;
|
||||
refreshUser: () => Promise<void>;
|
||||
@@ -39,7 +39,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
||||
try {
|
||||
await tokenManager.initialize();
|
||||
|
||||
if (api.auth.isAuthenticated()) {
|
||||
if (authService.isAuthenticated()) {
|
||||
// Get user from token first (faster), then validate with API
|
||||
const tokenUser = tokenManager.getUserFromToken();
|
||||
if (tokenUser) {
|
||||
@@ -58,7 +58,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
||||
|
||||
// Validate with API and get complete profile
|
||||
try {
|
||||
const profile = await api.auth.getCurrentUser();
|
||||
const profile = await authService.getCurrentUser();
|
||||
setUser(profile);
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch user profile:', error);
|
||||
@@ -84,7 +84,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
||||
const tokenResponse = await authService.login({ email, password });
|
||||
|
||||
// After login, get user profile
|
||||
const profile = await api.auth.getCurrentUser();
|
||||
const profile = await authService.getCurrentUser();
|
||||
setUser(profile);
|
||||
} catch (error) {
|
||||
setIsLoading(false);
|
||||
@@ -101,7 +101,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
||||
const tokenResponse = await authService.register(data);
|
||||
|
||||
// After registration, get user profile
|
||||
const profile = await api.auth.getCurrentUser();
|
||||
const profile = await authService.getCurrentUser();
|
||||
setUser(profile);
|
||||
} catch (error) {
|
||||
setIsLoading(false);
|
||||
@@ -139,10 +139,10 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
||||
}, [user]);
|
||||
|
||||
const refreshUser = useCallback(async () => {
|
||||
if (!api.auth.isAuthenticated()) return;
|
||||
if (!authService.isAuthenticated()) return;
|
||||
|
||||
try {
|
||||
const profile = await api.auth.getCurrentUser();
|
||||
const profile = await authService.getCurrentUser();
|
||||
setUser(profile);
|
||||
} catch (error) {
|
||||
console.error('User refresh error:', error);
|
||||
@@ -175,7 +175,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
||||
if (!user) return;
|
||||
|
||||
const checkTokenValidity = () => {
|
||||
if (!api.auth.isAuthenticated()) {
|
||||
if (!authService.isAuthenticated()) {
|
||||
console.warn('Token became invalid, logging out user');
|
||||
logout();
|
||||
}
|
||||
@@ -188,7 +188,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
||||
|
||||
const contextValue = {
|
||||
user,
|
||||
isAuthenticated: !!user && api.auth.isAuthenticated(),
|
||||
isAuthenticated: !!user && authService.isAuthenticated(),
|
||||
isLoading,
|
||||
login,
|
||||
register,
|
||||
|
||||
Reference in New Issue
Block a user