Add new frontend - fix 15
This commit is contained in:
@@ -52,7 +52,7 @@ export class AuthService {
|
|||||||
*/
|
*/
|
||||||
async register(userData: RegisterRequest): Promise<TokenResponse> {
|
async register(userData: RegisterRequest): Promise<TokenResponse> {
|
||||||
const response = await apiClient.post<ApiResponse<TokenResponse>>(
|
const response = await apiClient.post<ApiResponse<TokenResponse>>(
|
||||||
'/auth/register',
|
'/api/v1/auth/register',
|
||||||
userData
|
userData
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -1,19 +1,19 @@
|
|||||||
// frontend/src/contexts/AuthContext.tsx - FIXED VERSION
|
// frontend/src/contexts/AuthContext.tsx - FIXED VERSION
|
||||||
import React, { createContext, useContext, useEffect, useState, useCallback } from 'react';
|
import React, { createContext, useContext, useEffect, useState, useCallback } from 'react';
|
||||||
|
// FIXED: Import authService directly, not through the index
|
||||||
import { authService } from '../api/services/authService';
|
import { authService } from '../api/services/authService';
|
||||||
import { tokenManager } from '../api/auth/tokenManager';
|
import { tokenManager } from '../api/auth/tokenManager';
|
||||||
import {
|
import {
|
||||||
UserProfile,
|
UserProfile,
|
||||||
RegisterRequest,
|
RegisterRequest,
|
||||||
api
|
} from '../api/types/api';
|
||||||
} from '../api/services';
|
|
||||||
|
|
||||||
interface AuthContextType {
|
interface AuthContextType {
|
||||||
user: UserProfile | null;
|
user: UserProfile | null;
|
||||||
isAuthenticated: boolean;
|
isAuthenticated: boolean;
|
||||||
isLoading: boolean;
|
isLoading: boolean;
|
||||||
login: (email: string, password: string) => Promise<void>;
|
login: (email: string, password: string) => Promise<void>;
|
||||||
register: (data: RegisterRequest) => Promise<void>; // FIXED: Use RegisterRequest
|
register: (data: RegisterRequest) => Promise<void>;
|
||||||
logout: () => Promise<void>;
|
logout: () => Promise<void>;
|
||||||
updateProfile: (updates: Partial<UserProfile>) => Promise<void>;
|
updateProfile: (updates: Partial<UserProfile>) => Promise<void>;
|
||||||
refreshUser: () => Promise<void>;
|
refreshUser: () => Promise<void>;
|
||||||
@@ -39,7 +39,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
|||||||
try {
|
try {
|
||||||
await tokenManager.initialize();
|
await tokenManager.initialize();
|
||||||
|
|
||||||
if (api.auth.isAuthenticated()) {
|
if (authService.isAuthenticated()) {
|
||||||
// Get user from token first (faster), then validate with API
|
// Get user from token first (faster), then validate with API
|
||||||
const tokenUser = tokenManager.getUserFromToken();
|
const tokenUser = tokenManager.getUserFromToken();
|
||||||
if (tokenUser) {
|
if (tokenUser) {
|
||||||
@@ -58,7 +58,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
|||||||
|
|
||||||
// Validate with API and get complete profile
|
// Validate with API and get complete profile
|
||||||
try {
|
try {
|
||||||
const profile = await api.auth.getCurrentUser();
|
const profile = await authService.getCurrentUser();
|
||||||
setUser(profile);
|
setUser(profile);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to fetch user profile:', 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 });
|
const tokenResponse = await authService.login({ email, password });
|
||||||
|
|
||||||
// After login, get user profile
|
// After login, get user profile
|
||||||
const profile = await api.auth.getCurrentUser();
|
const profile = await authService.getCurrentUser();
|
||||||
setUser(profile);
|
setUser(profile);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
@@ -101,7 +101,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
|||||||
const tokenResponse = await authService.register(data);
|
const tokenResponse = await authService.register(data);
|
||||||
|
|
||||||
// After registration, get user profile
|
// After registration, get user profile
|
||||||
const profile = await api.auth.getCurrentUser();
|
const profile = await authService.getCurrentUser();
|
||||||
setUser(profile);
|
setUser(profile);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
@@ -139,10 +139,10 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
|||||||
}, [user]);
|
}, [user]);
|
||||||
|
|
||||||
const refreshUser = useCallback(async () => {
|
const refreshUser = useCallback(async () => {
|
||||||
if (!api.auth.isAuthenticated()) return;
|
if (!authService.isAuthenticated()) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const profile = await api.auth.getCurrentUser();
|
const profile = await authService.getCurrentUser();
|
||||||
setUser(profile);
|
setUser(profile);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('User refresh error:', error);
|
console.error('User refresh error:', error);
|
||||||
@@ -175,7 +175,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
|||||||
if (!user) return;
|
if (!user) return;
|
||||||
|
|
||||||
const checkTokenValidity = () => {
|
const checkTokenValidity = () => {
|
||||||
if (!api.auth.isAuthenticated()) {
|
if (!authService.isAuthenticated()) {
|
||||||
console.warn('Token became invalid, logging out user');
|
console.warn('Token became invalid, logging out user');
|
||||||
logout();
|
logout();
|
||||||
}
|
}
|
||||||
@@ -188,7 +188,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
|||||||
|
|
||||||
const contextValue = {
|
const contextValue = {
|
||||||
user,
|
user,
|
||||||
isAuthenticated: !!user && api.auth.isAuthenticated(),
|
isAuthenticated: !!user && authService.isAuthenticated(),
|
||||||
isLoading,
|
isLoading,
|
||||||
login,
|
login,
|
||||||
register,
|
register,
|
||||||
|
|||||||
@@ -54,6 +54,10 @@ class AuthMiddleware(BaseHTTPMiddleware):
|
|||||||
async def dispatch(self, request: Request, call_next) -> Response:
|
async def dispatch(self, request: Request, call_next) -> Response:
|
||||||
"""Process request with enhanced authentication"""
|
"""Process request with enhanced authentication"""
|
||||||
|
|
||||||
|
# Skip authentication for OPTIONS requests (CORS preflight)
|
||||||
|
if request.method == "OPTIONS":
|
||||||
|
return await call_next(request)
|
||||||
|
|
||||||
# Skip authentication for public routes
|
# Skip authentication for public routes
|
||||||
if self._is_public_route(request.url.path):
|
if self._is_public_route(request.url.path):
|
||||||
return await call_next(request)
|
return await call_next(request)
|
||||||
|
|||||||
@@ -44,6 +44,19 @@ class AuthProxy:
|
|||||||
) -> Response:
|
) -> Response:
|
||||||
"""Forward request to auth service with proper error handling"""
|
"""Forward request to auth service with proper error handling"""
|
||||||
|
|
||||||
|
# Handle OPTIONS requests directly for CORS
|
||||||
|
if request.method == "OPTIONS":
|
||||||
|
return Response(
|
||||||
|
status_code=200,
|
||||||
|
headers={
|
||||||
|
"Access-Control-Allow-Origin": settings.CORS_ORIGINS_LIST,
|
||||||
|
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
|
||||||
|
"Access-Control-Allow-Headers": "Content-Type, Authorization, X-Tenant-ID",
|
||||||
|
"Access-Control-Allow-Credentials": "true",
|
||||||
|
"Access-Control-Max-Age": "86400" # Cache preflight for 24 hours
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Get auth service URL (with service discovery if available)
|
# Get auth service URL (with service discovery if available)
|
||||||
auth_url = await self._get_auth_service_url()
|
auth_url = await self._get_auth_service_url()
|
||||||
|
|||||||
Reference in New Issue
Block a user