Implement secure user authentication with role-based access control

Integrates user authentication using bcrypt, session management with PostgreSQL, and role-based access control for enhanced security and user management.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 91318273-c764-4fd4-be04-bdc12c38af32
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/0715008c-7244-40f4-befc-26c014852236/ab880d72-1a34-4d36-b5be-9f44df85ed1a.jpg
This commit is contained in:
michaelschal committed 2025-07-09 18:44:20 +00:00
1 parent 42b249e5b0
commit 5c01234fc4
16 files changed
+2384 -827

No files matched your search

+67
View File
@@ -0,0 +1,67 @@
import { useState, useEffect } from "react";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { apiRequest } from "@/lib/queryClient";
import type { User, LoginData } from "@shared/schema";
export function useAuth() {
const queryClient = useQueryClient();
const [user, setUser] = useState<User | null>(null);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
const checkAuth = async () => {
try {
const response = await fetch('/api/auth/me', {
credentials: 'include',
});
if (response.ok) {
const userData = await response.json();
setUser(userData);
} else {
setUser(null);
}
} catch (error) {
setUser(null);
} finally {
setIsLoading(false);
}
};
checkAuth();
}, []);
const loginMutation = useMutation({
mutationFn: async (credentials: LoginData) => {
const response = await apiRequest('/api/auth/login', {
method: 'POST',
body: JSON.stringify(credentials)
});
return response.json();
},
onSuccess: (data) => {
setUser(data.user);
queryClient.clear();
},
});
const logoutMutation = useMutation({
mutationFn: async () => {
await apiRequest('/api/auth/logout', { method: 'POST' });
},
onSuccess: () => {
setUser(null);
queryClient.clear();
},
});
return {
user,
isLoading,
isAuthenticated: !!user,
login: loginMutation.mutateAsync,
logout: logoutMutation.mutateAsync,
isLoggingIn: loginMutation.isPending,
isLoggingOut: logoutMutation.isPending,
};
}