Add local user authentication and admin capabilities
Implement local user authentication with password hashing, add admin roles for user management and audit log viewing, and introduce audit logging for critical actions. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 776963d0-f75d-42e2-a57b-cc36bdff8495 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: 832a44ff-12ae-4096-8a0d-666ec083d536 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/0683fb79-a27c-485c-9333-5f4b288c4567/776963d0-f75d-42e2-a57b-cc36bdff8495/1p7jhzu Replit-Helium-Checkpoint-Created: true
This commit is contained in:
@@ -1,32 +1,51 @@
|
||||
import { useGetMe } from "@workspace/api-client-react";
|
||||
|
||||
export type AuthUser = {
|
||||
sub: string;
|
||||
email?: string | null;
|
||||
name?: string | null;
|
||||
preferredUsername?: string | null;
|
||||
};
|
||||
import { useGetMe, useGetAuthMode, getGetMeQueryKey, getGetAuthModeQueryKey } from "@workspace/api-client-react";
|
||||
|
||||
export function useAuth() {
|
||||
const { data: user, isLoading, error } = useGetMe({
|
||||
query: {
|
||||
queryKey: getGetMeQueryKey(),
|
||||
retry: false,
|
||||
staleTime: 1000 * 60 * 5,
|
||||
},
|
||||
});
|
||||
|
||||
const { data: authMode } = useGetAuthMode({
|
||||
query: {
|
||||
queryKey: getGetAuthModeQueryKey(),
|
||||
staleTime: Infinity,
|
||||
retry: false,
|
||||
},
|
||||
});
|
||||
|
||||
const isAuthenticated = !!user && !error;
|
||||
const isAdmin = isAuthenticated && user?.role === "admin";
|
||||
const isLocalMode = authMode?.mode === "local";
|
||||
|
||||
function login(returnTo?: string) {
|
||||
const url = returnTo
|
||||
? `/api/auth/login?returnTo=${encodeURIComponent(returnTo)}`
|
||||
: "/api/auth/login";
|
||||
window.location.href = url;
|
||||
if (isLocalMode) {
|
||||
const path = returnTo
|
||||
? `/login?returnTo=${encodeURIComponent(returnTo)}`
|
||||
: "/login";
|
||||
window.location.href = path;
|
||||
} else {
|
||||
const url = returnTo
|
||||
? `/api/auth/login?returnTo=${encodeURIComponent(returnTo)}`
|
||||
: "/api/auth/login";
|
||||
window.location.href = url;
|
||||
}
|
||||
}
|
||||
|
||||
function logout() {
|
||||
window.location.href = "/api/auth/logout";
|
||||
}
|
||||
|
||||
return { user: isAuthenticated ? user : null, isLoading, isAuthenticated, login, logout };
|
||||
return {
|
||||
user: isAuthenticated ? user : null,
|
||||
isLoading,
|
||||
isAuthenticated,
|
||||
isAdmin,
|
||||
isLocalMode,
|
||||
login,
|
||||
logout,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user