- Implemented Issue #9: User Authentication & Authorization - Added User model fields (FailedLoginAttempts, LockedUntil, LastLogin) - Created AuthService with JWT support, bcrypt hashing, and account lockout - Added AuthMiddleware and AuthHandler - Registered auth routes in backend - Created AuthContext and RequireAuth component in frontend - Implemented Login page and integrated with backend - Fixed 'Blank Page' issue in local Docker environment - Added QueryClientProvider to main.tsx - Installed missing lucide-react dependency - Fixed TypeScript linting errors in SetupGuard.tsx - Updated docker-entrypoint.sh to use 127.0.0.1 for reliable Caddy checks - Verified with local Docker build
72 lines
1.8 KiB
TypeScript
72 lines
1.8 KiB
TypeScript
import React, { createContext, useContext, useState, useEffect } from 'react';
|
|
import client from '../api/client';
|
|
import { AxiosResponse } from 'axios';
|
|
|
|
interface User {
|
|
user_id: number;
|
|
role: string;
|
|
}
|
|
|
|
interface AuthContextType {
|
|
user: User | null;
|
|
login: () => void;
|
|
logout: () => void;
|
|
isAuthenticated: boolean;
|
|
isLoading: boolean;
|
|
}
|
|
|
|
const AuthContext = createContext<AuthContextType | undefined>(undefined);
|
|
|
|
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
|
const [user, setUser] = useState<User | null>(null);
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
const checkAuth = async () => {
|
|
try {
|
|
const response = await client.get('/auth/me');
|
|
setUser(response.data);
|
|
} catch (error) {
|
|
setUser(null);
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
checkAuth();
|
|
}, []);
|
|
|
|
const login = () => {
|
|
// Token is stored in cookie by backend, but we might want to store it in memory or trigger a re-fetch
|
|
// Actually, if backend sets cookie, we just need to fetch /auth/me
|
|
client.get('/auth/me').then((response: AxiosResponse<User>) => {
|
|
setUser(response.data);
|
|
}).catch(() => {
|
|
setUser(null);
|
|
});
|
|
};
|
|
|
|
const logout = async () => {
|
|
try {
|
|
await client.post('/auth/logout');
|
|
} catch (error) {
|
|
console.error("Logout failed", error);
|
|
}
|
|
setUser(null);
|
|
};
|
|
|
|
return (
|
|
<AuthContext.Provider value={{ user, login, logout, isAuthenticated: !!user, isLoading }}>
|
|
{children}
|
|
</AuthContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const useAuth = () => {
|
|
const context = useContext(AuthContext);
|
|
if (context === undefined) {
|
|
throw new Error('useAuth must be used within an AuthProvider');
|
|
}
|
|
return context;
|
|
};
|