Added LDAP config and auth
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import { create } from 'zustand';
|
||||
import api from '../api/axiosConfig';
|
||||
|
||||
const useAuthStore = create((set) => ({
|
||||
isAuthenticated: false,
|
||||
isLoading: true,
|
||||
error: null,
|
||||
user: null,
|
||||
|
||||
login: async (username, password) => {
|
||||
set({ isLoading: true, error: null });
|
||||
try {
|
||||
const res = await api.post('login/', { username, password });
|
||||
set({
|
||||
isAuthenticated: true,
|
||||
isLoading: false,
|
||||
user: {
|
||||
username: res.data.username,
|
||||
firstName: res.data.first_name,
|
||||
lastName: res.data.last_name,
|
||||
email: res.data.email,
|
||||
fullName: [res.data.first_name, res.data.last_name].filter(Boolean).join(' ')
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
// PROPER USAGE: Check if Django sent a specific error string, otherwise default
|
||||
const backendError = err.response?.data?.error || 'Invalid network credentials';
|
||||
set({ error: backendError, isLoading: false, isAuthenticated: false });
|
||||
}
|
||||
},
|
||||
|
||||
logout: async () => {
|
||||
try {
|
||||
await api.post('logout/');
|
||||
} catch (err) {
|
||||
console.error("Logout error:", err);
|
||||
} finally {
|
||||
set({ isAuthenticated: false, user: null, error: null });
|
||||
}
|
||||
},
|
||||
|
||||
checkAuth: async () => {
|
||||
try {
|
||||
// Hit the new lightweight endpoint
|
||||
const res = await api.get('session');
|
||||
|
||||
// Rehydrate the store with the user's info!
|
||||
set({
|
||||
isAuthenticated: true,
|
||||
isLoading: false,
|
||||
user: {
|
||||
username: res.data.username,
|
||||
firstName: res.data.first_name,
|
||||
lastName: res.data.last_name,
|
||||
email: res.data.email,
|
||||
fullName: [res.data.first_name, res.data.last_name].filter(Boolean).join(' ')
|
||||
}
|
||||
});
|
||||
} catch {
|
||||
// If it fails, ensure the store is completely wiped
|
||||
set({ isAuthenticated: false, isLoading: false, user: null });
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
export default useAuthStore;
|
||||
Reference in New Issue
Block a user