Initial commit: MinIO WebUI - Complete implementation
- Backend: Express.js API with MinIO CLI integration - Frontend: React with Material-UI for non-technical users - Features: Bucket management, user creation, storage monitoring - Security: JWT auth, IP filtering, encrypted passwords - Docker support for easy deployment - Automated weekly storage reports - Setup and deployment scripts included
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import { create } from 'zustand';
|
||||
import { persist } from 'zustand/middleware';
|
||||
import api from '../services/api';
|
||||
|
||||
interface User {
|
||||
role: string;
|
||||
loginTime: string;
|
||||
}
|
||||
|
||||
interface AuthState {
|
||||
isAuthenticated: boolean;
|
||||
user: User | null;
|
||||
loading: boolean;
|
||||
login: (password: string) => Promise<void>;
|
||||
logout: () => Promise<void>;
|
||||
checkAuth: () => Promise<void>;
|
||||
}
|
||||
|
||||
const useAuthStore = create<AuthState>()(
|
||||
persist(
|
||||
(set) => ({
|
||||
isAuthenticated: false,
|
||||
user: null,
|
||||
loading: false,
|
||||
|
||||
login: async (password: string) => {
|
||||
set({ loading: true });
|
||||
try {
|
||||
const response = await api.post('/auth/login', { password });
|
||||
const { token, role, loginTime } = response.data;
|
||||
|
||||
// Store token if needed
|
||||
if (token) {
|
||||
localStorage.setItem('token', token);
|
||||
}
|
||||
|
||||
set({
|
||||
isAuthenticated: true,
|
||||
user: { role, loginTime },
|
||||
loading: false,
|
||||
});
|
||||
} catch (error) {
|
||||
set({ loading: false });
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
logout: async () => {
|
||||
try {
|
||||
await api.post('/auth/logout');
|
||||
} catch (error) {
|
||||
console.error('Logout error:', error);
|
||||
} finally {
|
||||
localStorage.removeItem('token');
|
||||
set({
|
||||
isAuthenticated: false,
|
||||
user: null,
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
checkAuth: async () => {
|
||||
try {
|
||||
const response = await api.get('/auth/status');
|
||||
if (response.data.authenticated) {
|
||||
set({
|
||||
isAuthenticated: true,
|
||||
user: {
|
||||
role: response.data.role,
|
||||
loginTime: response.data.loginTime,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
set({
|
||||
isAuthenticated: false,
|
||||
user: null,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
set({
|
||||
isAuthenticated: false,
|
||||
user: null,
|
||||
});
|
||||
}
|
||||
},
|
||||
}),
|
||||
{
|
||||
name: 'auth-storage',
|
||||
partialize: (state) => ({ isAuthenticated: state.isAuthenticated }),
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
export default useAuthStore;
|
||||
Reference in New Issue
Block a user