mirror of
https://github.com/allankoechke/mantisbase-admin.git
synced 2026-11-29 09:16:42 +00:00
feat: fix auth loop issue
This commit is contained in:
@@ -31,7 +31,7 @@ import {
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog"
|
||||
import { ApiClient, SYS_ADMINS_API, type TableMetadata, type Admin, type AppSettings } from "@/lib/api"
|
||||
import { ApiClient, SYS_ADMINS_API, type TableMetadata, type Admin } from "@/lib/api"
|
||||
import { DatabaseSection } from "./database/database-section"
|
||||
import { AdminsSection } from "./admins/admins-section"
|
||||
import { SettingsSection } from "./settings/settings-section"
|
||||
@@ -42,7 +42,6 @@ import { ThemeToggle } from "./theme-toggle"
|
||||
import { useNavigate, useLocation } from "react-router-dom"
|
||||
import { useAuth } from "@/lib/auth"
|
||||
import { getDashboardSection, ROUTES } from "@/lib/routes"
|
||||
import { useAppState, type AppMode } from "@/lib/app-state"
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
export function AdminDashboard() {
|
||||
@@ -50,14 +49,12 @@ export function AdminDashboard() {
|
||||
const [tables, setTables] = React.useState<TableMetadata[]>([])
|
||||
const [admins, setAdmins] = React.useState<Admin[]>([])
|
||||
const [loading, setLoading] = React.useState(true)
|
||||
const [settings, setSettings] = React.useState<AppSettings | null>(null)
|
||||
const [authErrorDialog, setAuthErrorDialog] = React.useState(false)
|
||||
const [authErrorReason, setAuthErrorReason] = React.useState("") // Auth error reason
|
||||
const { toast } = useToast()
|
||||
const navigate = useNavigate()
|
||||
const location = useLocation()
|
||||
const { logout } = useAuth()
|
||||
const { mode } = useAppState()
|
||||
const { logout, refreshSession } = useAuth()
|
||||
|
||||
const showError = React.useCallback(
|
||||
(error: string, type: "error" | "warning" = "error") => {
|
||||
@@ -87,15 +84,19 @@ export function AdminDashboard() {
|
||||
[toast],
|
||||
)
|
||||
|
||||
const handleUnauthorized = React.useCallback((reason?: string | "") => {
|
||||
const handleUnauthorized = React.useCallback(async (reason?: string | "") => {
|
||||
try {
|
||||
setAuthErrorReason(reason || "") // Set the auth error string
|
||||
setAuthErrorDialog(true) // Set the auth dialog to open
|
||||
// Don't call handleLogout() here - let the dialog handle navigation
|
||||
const stillAuthenticated = await refreshSession()
|
||||
if (stillAuthenticated) {
|
||||
return
|
||||
}
|
||||
|
||||
setAuthErrorReason(reason || "")
|
||||
setAuthErrorDialog(true)
|
||||
} catch (error) {
|
||||
console.warn("Failed to handle unauthorized:", error)
|
||||
}
|
||||
}, [])
|
||||
}, [refreshSession])
|
||||
|
||||
const [apiClient, setApiClient] = React.useState(
|
||||
() => new ApiClient(handleUnauthorized, showError),
|
||||
@@ -144,20 +145,6 @@ export function AdminDashboard() {
|
||||
}, [location.pathname, loadAdmins])
|
||||
|
||||
const loadData = async () => {
|
||||
// Set default settings if loading fails
|
||||
setSettings({
|
||||
appName: "ACME Mantis Project",
|
||||
baseUrl: "http://127.0.0.1:7070",
|
||||
mantisVersion: "0.0.0",
|
||||
maintenanceMode: false,
|
||||
maxFileSize: 10,
|
||||
allowRegistration: true,
|
||||
emailVerificationRequired: false,
|
||||
sessionTimeout: 84000,
|
||||
adminSessionTimeout: 3600,
|
||||
mode: mode,
|
||||
})
|
||||
|
||||
try {
|
||||
setLoading(true)
|
||||
|
||||
@@ -207,14 +194,6 @@ export function AdminDashboard() {
|
||||
}
|
||||
}
|
||||
|
||||
const handleModeChange = (newMode: AppMode, baseUrl?: string) => {
|
||||
// Update API client when mode changes (mode is handled via app state)
|
||||
const newApiClient = new ApiClient(handleUnauthorized, showError)
|
||||
setApiClient(newApiClient)
|
||||
|
||||
// Reload data with new mode
|
||||
loadData()
|
||||
}
|
||||
|
||||
const sidebarItems = [
|
||||
{
|
||||
@@ -370,14 +349,7 @@ export function AdminDashboard() {
|
||||
)}
|
||||
{currentSection === "logs" && <LogsSection apiClient={apiClient} />}
|
||||
{currentSection === "sync" && <SyncSection />}
|
||||
{currentSection === "settings" && (
|
||||
<SettingsSection
|
||||
apiClient={apiClient}
|
||||
settings={settings}
|
||||
onSettingsUpdate={setSettings}
|
||||
onModeChange={handleModeChange}
|
||||
/>
|
||||
)}
|
||||
{currentSection === "settings" && <SettingsSection apiClient={apiClient} />}
|
||||
</>
|
||||
)}
|
||||
</main>
|
||||
|
||||
@@ -7,123 +7,218 @@ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/com
|
||||
import { Input } from "@/components/ui/input"
|
||||
import { Label } from "@/components/ui/label"
|
||||
import { Switch } from "@/components/ui/switch"
|
||||
import { SYS_SETTINGS_CONFIG_API, type ApiClient, type AppSettings } from "@/lib/api"
|
||||
import { Textarea } from "@/components/ui/textarea"
|
||||
import {
|
||||
SYS_SETTINGS_CONFIG_API,
|
||||
type ApiClient,
|
||||
type AppSettings,
|
||||
type SmtpConfig,
|
||||
} from "@/lib/api"
|
||||
import { useToast } from "@/hooks/use-toast"
|
||||
import { useAppState, type AppMode } from "@/lib/app-state"
|
||||
|
||||
interface SettingsSectionProps {
|
||||
apiClient: ApiClient
|
||||
settings: AppSettings | null
|
||||
onSettingsUpdate: (settings: AppSettings) => void
|
||||
onModeChange: (mode: AppMode, baseUrl?: string) => void
|
||||
}
|
||||
|
||||
export function SettingsSection({ apiClient, settings, onSettingsUpdate, onModeChange }: SettingsSectionProps) {
|
||||
type SettingsCard = "general" | "file" | "features" | "smtp"
|
||||
|
||||
const BYTES_PER_MB = 1024 * 1024
|
||||
|
||||
function corsOriginsToText(origins: string[]): string {
|
||||
return origins.join("\n")
|
||||
}
|
||||
|
||||
function textToCorsOrigins(text: string): string[] {
|
||||
return text.split("\n").map((line) => line.trim()).filter(Boolean)
|
||||
}
|
||||
|
||||
function smtpEqual(a: SmtpConfig, b: SmtpConfig, ignorePassword = false): boolean {
|
||||
return (
|
||||
a.host === b.host &&
|
||||
a.port === b.port &&
|
||||
a.user === b.user &&
|
||||
a.from === b.from &&
|
||||
a.tls === b.tls &&
|
||||
(ignorePassword || a.password === b.password)
|
||||
)
|
||||
}
|
||||
|
||||
export function SettingsSection({ apiClient }: SettingsSectionProps) {
|
||||
const { toast } = useToast()
|
||||
const { mode, setMode } = useAppState()
|
||||
const [formData, setFormData] = React.useState<AppSettings | null>(null)
|
||||
const [originalData, setOriginalData] = React.useState<AppSettings | null>(null)
|
||||
const [corsOriginsText, setCorsOriginsText] = React.useState("")
|
||||
const [originalCorsOriginsText, setOriginalCorsOriginsText] = React.useState("")
|
||||
const [isLoading, setIsLoading] = React.useState<Record<string, boolean>>({})
|
||||
const [isRefreshing, setIsRefreshing] = React.useState(false)
|
||||
const [isBootstrapping, setIsBootstrapping] = React.useState(true)
|
||||
|
||||
const applySettings = React.useCallback((settings: AppSettings) => {
|
||||
const normalized: AppSettings = {
|
||||
...settings,
|
||||
corsAllowedOrigins: settings.corsAllowedOrigins ?? [],
|
||||
smtp: {
|
||||
host: "",
|
||||
port: 587,
|
||||
user: "",
|
||||
password: "",
|
||||
from: "",
|
||||
tls: true,
|
||||
...settings.smtp,
|
||||
},
|
||||
}
|
||||
const corsText = corsOriginsToText(normalized.corsAllowedOrigins)
|
||||
setFormData(normalized)
|
||||
setOriginalData(normalized)
|
||||
setCorsOriginsText(corsText)
|
||||
setOriginalCorsOriginsText(corsText)
|
||||
}, [])
|
||||
|
||||
const loadSettings = React.useCallback(async () => {
|
||||
const settings = await apiClient.call<AppSettings>(SYS_SETTINGS_CONFIG_API)
|
||||
applySettings(settings)
|
||||
return settings
|
||||
}, [apiClient, applySettings])
|
||||
|
||||
React.useEffect(() => {
|
||||
if (settings) {
|
||||
const data = { ...settings, mode }
|
||||
setFormData(data)
|
||||
setOriginalData(data)
|
||||
}
|
||||
}, [settings, mode])
|
||||
let cancelled = false
|
||||
|
||||
// Track changes per card section
|
||||
const getCardChanges = (cardType: "general" | "file" | "features"): boolean => {
|
||||
const bootstrap = async () => {
|
||||
try {
|
||||
await loadSettings()
|
||||
} catch (error) {
|
||||
if (!cancelled) {
|
||||
console.error("Failed to load settings:", error)
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Error",
|
||||
description: "Failed to load settings. Please try again.",
|
||||
})
|
||||
}
|
||||
} finally {
|
||||
if (!cancelled) {
|
||||
setIsBootstrapping(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bootstrap()
|
||||
|
||||
return () => {
|
||||
cancelled = true
|
||||
}
|
||||
}, [loadSettings, toast])
|
||||
|
||||
const getCardChanges = (cardType: SettingsCard): boolean => {
|
||||
if (!formData || !originalData) return false
|
||||
|
||||
|
||||
if (cardType === "general") {
|
||||
return (
|
||||
formData.appName !== originalData.appName ||
|
||||
formData.baseUrl !== originalData.baseUrl
|
||||
formData.orgName !== originalData.orgName ||
|
||||
formData.siteDomain !== originalData.siteDomain ||
|
||||
corsOriginsText !== originalCorsOriginsText
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
if (cardType === "file") {
|
||||
return (
|
||||
formData.maxFileSize !== originalData.maxFileSize ||
|
||||
formData.logRetentionDays !== originalData.logRetentionDays ||
|
||||
formData.sessionTimeout !== originalData.sessionTimeout ||
|
||||
formData.adminSessionTimeout !== originalData.adminSessionTimeout
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
if (cardType === "features") {
|
||||
return (
|
||||
formData.maintenanceMode !== originalData.maintenanceMode ||
|
||||
formData.allowRegistration !== originalData.allowRegistration ||
|
||||
formData.emailVerificationRequired !== originalData.emailVerificationRequired
|
||||
formData.disableAdminRegistration !== originalData.disableAdminRegistration ||
|
||||
formData.disableSchemaMutations !== originalData.disableSchemaMutations ||
|
||||
formData.emailVerificationRequired !== originalData.emailVerificationRequired ||
|
||||
formData.jwtEnableSetIssuer !== originalData.jwtEnableSetIssuer ||
|
||||
formData.jwtEnableSetAudience !== originalData.jwtEnableSetAudience
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
if (cardType === "smtp") {
|
||||
return !smtpEqual(formData.smtp, originalData.smtp)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
const resetCardChanges = (cardType: "general" | "file" | "features") => {
|
||||
if (!formData || !originalData) return
|
||||
|
||||
const buildPatchPayload = (cardType: SettingsCard): Partial<AppSettings> => {
|
||||
if (!formData) return {}
|
||||
|
||||
if (cardType === "general") {
|
||||
setFormData({
|
||||
...formData,
|
||||
appName: originalData.appName,
|
||||
baseUrl: originalData.baseUrl,
|
||||
})
|
||||
return {
|
||||
orgName: formData.orgName,
|
||||
siteDomain: formData.siteDomain,
|
||||
corsAllowedOrigins: textToCorsOrigins(corsOriginsText),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (cardType === "file") {
|
||||
setFormData({
|
||||
...formData,
|
||||
maxFileSize: originalData.maxFileSize,
|
||||
sessionTimeout: originalData.sessionTimeout,
|
||||
adminSessionTimeout: originalData.adminSessionTimeout,
|
||||
})
|
||||
return {
|
||||
maxFileSize: formData.maxFileSize,
|
||||
logRetentionDays: formData.logRetentionDays,
|
||||
sessionTimeout: formData.sessionTimeout,
|
||||
adminSessionTimeout: formData.adminSessionTimeout,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (cardType === "features") {
|
||||
setFormData({
|
||||
...formData,
|
||||
maintenanceMode: originalData.maintenanceMode,
|
||||
allowRegistration: originalData.allowRegistration,
|
||||
emailVerificationRequired: originalData.emailVerificationRequired,
|
||||
})
|
||||
return {
|
||||
disableAdminRegistration: formData.disableAdminRegistration,
|
||||
disableSchemaMutations: formData.disableSchemaMutations,
|
||||
emailVerificationRequired: formData.emailVerificationRequired,
|
||||
jwtEnableSetIssuer: formData.jwtEnableSetIssuer,
|
||||
jwtEnableSetAudience: formData.jwtEnableSetAudience,
|
||||
}
|
||||
}
|
||||
|
||||
const smtpPatch: Partial<SmtpConfig> = {
|
||||
host: formData.smtp.host,
|
||||
port: formData.smtp.port,
|
||||
user: formData.smtp.user,
|
||||
from: formData.smtp.from,
|
||||
tls: formData.smtp.tls,
|
||||
}
|
||||
|
||||
if (formData.smtp.password.length > 0) {
|
||||
smtpPatch.password = formData.smtp.password
|
||||
}
|
||||
|
||||
return { smtp: smtpPatch as SmtpConfig }
|
||||
}
|
||||
|
||||
const handleInputChange = (field: keyof AppSettings, value: any) => {
|
||||
const handleInputChange = <K extends keyof AppSettings>(field: K, value: AppSettings[K]) => {
|
||||
if (!formData) return
|
||||
setFormData({ ...formData, [field]: value })
|
||||
}
|
||||
|
||||
const handleSmtpChange = <K extends keyof SmtpConfig>(field: K, value: SmtpConfig[K]) => {
|
||||
if (!formData) return
|
||||
setFormData({
|
||||
...formData,
|
||||
[field]: value,
|
||||
smtp: { ...formData.smtp, [field]: value },
|
||||
})
|
||||
}
|
||||
|
||||
const handleSave = async (cardType: "general" | "file" | "features") => {
|
||||
const handleSave = async (cardType: SettingsCard) => {
|
||||
if (!formData) return
|
||||
|
||||
setIsLoading({ ...isLoading, [cardType]: true })
|
||||
setIsLoading((prev) => ({ ...prev, [cardType]: true }))
|
||||
try {
|
||||
const updatedSettings = await apiClient.call<AppSettings>(SYS_SETTINGS_CONFIG_API, {
|
||||
method: "PATCH",
|
||||
body: JSON.stringify(formData),
|
||||
body: JSON.stringify(buildPatchPayload(cardType)),
|
||||
})
|
||||
|
||||
// If the request failed, throw the error here
|
||||
if (updatedSettings?.error?.length > 0) throw updatedSettings.error
|
||||
|
||||
const data = { ...updatedSettings, mode }
|
||||
setFormData(data)
|
||||
setOriginalData(data)
|
||||
onSettingsUpdate(updatedSettings)
|
||||
applySettings(updatedSettings)
|
||||
|
||||
toast({
|
||||
title: "Settings Saved",
|
||||
description: `${cardType.charAt(0).toUpperCase() + cardType.slice(1)} settings have been updated successfully.`,
|
||||
description: `${cardType.charAt(0).toUpperCase()}${cardType.slice(1)} settings have been updated successfully.`,
|
||||
duration: 3000,
|
||||
})
|
||||
} catch (error) {
|
||||
@@ -134,32 +229,56 @@ export function SettingsSection({ apiClient, settings, onSettingsUpdate, onModeC
|
||||
description: "Failed to save settings. Please try again.",
|
||||
})
|
||||
} finally {
|
||||
setIsLoading({ ...isLoading, [cardType]: false })
|
||||
setIsLoading((prev) => ({ ...prev, [cardType]: false }))
|
||||
}
|
||||
}
|
||||
|
||||
const handleRefresh = async () => {
|
||||
setIsRefreshing(true)
|
||||
try {
|
||||
const updatedSettings = await apiClient.call<AppSettings>(SYS_SETTINGS_CONFIG_API)
|
||||
|
||||
// If the request failed, throw the error here
|
||||
if (updatedSettings?.error?.length > 0) throw updatedSettings.error
|
||||
|
||||
onSettingsUpdate(updatedSettings)
|
||||
await loadSettings()
|
||||
} catch (error) {
|
||||
console.error("Failed to refresh settings:", error)
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Error",
|
||||
description: "Failed to refresh settings. Please try again.",
|
||||
})
|
||||
} finally {
|
||||
setIsRefreshing(false)
|
||||
}
|
||||
}
|
||||
|
||||
// Show loading if we don't have formData yet
|
||||
if (!formData) {
|
||||
const renderSaveButton = (cardType: SettingsCard) => {
|
||||
if (!getCardChanges(cardType)) return null
|
||||
|
||||
return (
|
||||
<div className="flex items-center justify-center h-64">
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => handleSave(cardType)}
|
||||
disabled={isLoading[cardType]}
|
||||
className="gap-2"
|
||||
>
|
||||
{isLoading[cardType] ? (
|
||||
<>
|
||||
<RefreshCw className="h-4 w-4 animate-spin" />
|
||||
Saving...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Save className="h-4 w-4" />
|
||||
Save
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
|
||||
if (isBootstrapping || !formData) {
|
||||
return (
|
||||
<div className="flex h-64 items-center justify-center">
|
||||
<div className="text-center">
|
||||
<div className="h-8 w-8 animate-spin rounded-full border-4 border-primary border-t-transparent mx-auto mb-4"></div>
|
||||
<div className="mx-auto mb-4 h-8 w-8 animate-spin rounded-full border-4 border-primary border-t-transparent" />
|
||||
<p className="text-muted-foreground">Loading settings...</p>
|
||||
</div>
|
||||
</div>
|
||||
@@ -168,7 +287,6 @@ export function SettingsSection({ apiClient, settings, onSettingsUpdate, onModeC
|
||||
|
||||
return (
|
||||
<div className="space-y-8 p-6">
|
||||
{/* Header */}
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="space-y-1">
|
||||
<h1 className="text-3xl font-bold tracking-tight">Settings</h1>
|
||||
@@ -180,212 +298,261 @@ export function SettingsSection({ apiClient, settings, onSettingsUpdate, onModeC
|
||||
</div>
|
||||
|
||||
<div className="space-y-6">
|
||||
{/* General Settings */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<CardTitle>General</CardTitle>
|
||||
<CardDescription>Basic application information and configuration</CardDescription>
|
||||
<CardDescription>Organization details, public site URL, and CORS origins</CardDescription>
|
||||
</div>
|
||||
{getCardChanges("general") && (
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => handleSave("general")}
|
||||
disabled={isLoading.general}
|
||||
className="gap-2"
|
||||
>
|
||||
{isLoading.general ? (
|
||||
<>
|
||||
<RefreshCw className="h-4 w-4 animate-spin" />
|
||||
Saving...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Save className="h-4 w-4" />
|
||||
Save
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
)}
|
||||
{renderSaveButton("general")}
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-6">
|
||||
<div className="grid gap-4 sm:grid-cols-2">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="app-name">Application Name</Label>
|
||||
<Input
|
||||
id="app-name"
|
||||
value={formData?.appName || ""}
|
||||
onChange={(e) => handleInputChange("appName", e.target.value)}
|
||||
placeholder="My Application"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="version">Version</Label>
|
||||
<Input
|
||||
id="version"
|
||||
value={formData?.mantisVersion || ""}
|
||||
disabled
|
||||
className="bg-muted"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<CardContent className="space-y-6">
|
||||
<div className="grid gap-4 sm:grid-cols-2">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="base-url">Base URL</Label>
|
||||
<Label htmlFor="org-name">Organization Name</Label>
|
||||
<Input
|
||||
id="base-url"
|
||||
value={formData?.baseUrl || ""}
|
||||
onChange={(e) => handleInputChange("baseUrl", e.target.value)}
|
||||
placeholder="https://your-api-domain.com"
|
||||
id="org-name"
|
||||
value={formData.orgName}
|
||||
onChange={(e) => handleInputChange("orgName", e.target.value)}
|
||||
placeholder="ACME Corp"
|
||||
/>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{mode === "PROD"
|
||||
? "The base URL for your API endpoints in production mode"
|
||||
: "Not used in test mode - API calls go to external server"}
|
||||
</p>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="site-domain">Site Domain</Label>
|
||||
<Input
|
||||
id="site-domain"
|
||||
value={formData.siteDomain}
|
||||
onChange={(e) => handleInputChange("siteDomain", e.target.value)}
|
||||
placeholder="https://acme.example.com"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="cors-origins">CORS Allowed Origins</Label>
|
||||
<Textarea
|
||||
id="cors-origins"
|
||||
value={corsOriginsText}
|
||||
onChange={(e) => setCorsOriginsText(e.target.value)}
|
||||
placeholder={"http://localhost:3000\nhttp://127.0.0.1:3000"}
|
||||
rows={4}
|
||||
/>
|
||||
<p className="text-sm text-muted-foreground">Enter one origin per line.</p>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* File & Session Settings */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<CardTitle>File & Sessions</CardTitle>
|
||||
<CardDescription>Configure file upload limits and session timeouts</CardDescription>
|
||||
<CardDescription>Upload limits, log retention, and session timeouts</CardDescription>
|
||||
</div>
|
||||
{getCardChanges("file") && (
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => handleSave("file")}
|
||||
disabled={isLoading.file}
|
||||
className="gap-2"
|
||||
>
|
||||
{isLoading.file ? (
|
||||
<>
|
||||
<RefreshCw className="h-4 w-4 animate-spin" />
|
||||
Saving...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Save className="h-4 w-4" />
|
||||
Save
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
)}
|
||||
{renderSaveButton("file")}
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-6">
|
||||
<div className="grid gap-4 sm:grid-cols-3">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="max-file-size">Max File Size (MB)</Label>
|
||||
<Input
|
||||
id="max-file-size"
|
||||
type="number"
|
||||
value={formData?.maxFileSize?.toString() || 10}
|
||||
onChange={(e) => handleInputChange("maxFileSize", Number.parseInt(e.target.value) || 10)}
|
||||
min="1"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="session-timeout">Session Timeout (s)</Label>
|
||||
<Input
|
||||
id="session-timeout"
|
||||
type="number"
|
||||
value={formData?.sessionTimeout?.toString() || 86400}
|
||||
onChange={(e) => handleInputChange("sessionTimeout", Number.parseInt(e.target.value) || 86400)}
|
||||
min="1"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="admin-session-timeout">Admin Session (s)</Label>
|
||||
<Input
|
||||
id="admin-session-timeout"
|
||||
type="number"
|
||||
value={formData?.adminSessionTimeout?.toString() || 3600}
|
||||
onChange={(e) => handleInputChange("adminSessionTimeout", Number.parseInt(e.target.value) || 3600)}
|
||||
min="1"
|
||||
/>
|
||||
</div>
|
||||
<CardContent className="space-y-6">
|
||||
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="max-file-size">Max File Size (MB)</Label>
|
||||
<Input
|
||||
id="max-file-size"
|
||||
type="number"
|
||||
value={Math.round(formData.maxFileSize / BYTES_PER_MB)}
|
||||
onChange={(e) =>
|
||||
handleInputChange(
|
||||
"maxFileSize",
|
||||
(Number.parseInt(e.target.value, 10) || 1) * BYTES_PER_MB,
|
||||
)
|
||||
}
|
||||
min="1"
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="log-retention-days">Log Retention (days)</Label>
|
||||
<Input
|
||||
id="log-retention-days"
|
||||
type="number"
|
||||
value={formData.logRetentionDays}
|
||||
onChange={(e) =>
|
||||
handleInputChange("logRetentionDays", Number.parseInt(e.target.value, 10) || 1)
|
||||
}
|
||||
min="1"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="session-timeout">Session Timeout (s)</Label>
|
||||
<Input
|
||||
id="session-timeout"
|
||||
type="number"
|
||||
value={formData.sessionTimeout}
|
||||
onChange={(e) =>
|
||||
handleInputChange("sessionTimeout", Number.parseInt(e.target.value, 10) || 1)
|
||||
}
|
||||
min="1"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="admin-session-timeout">Admin Session (s)</Label>
|
||||
<Input
|
||||
id="admin-session-timeout"
|
||||
type="number"
|
||||
value={formData.adminSessionTimeout}
|
||||
onChange={(e) =>
|
||||
handleInputChange("adminSessionTimeout", Number.parseInt(e.target.value, 10) || 1)
|
||||
}
|
||||
min="1"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Feature Toggles */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<CardTitle>Features</CardTitle>
|
||||
<CardDescription>Enable or disable application features</CardDescription>
|
||||
<CardDescription>Registration, schema changes, email verification, and JWT options</CardDescription>
|
||||
</div>
|
||||
{getCardChanges("features") && (
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => handleSave("features")}
|
||||
disabled={isLoading.features}
|
||||
className="gap-2"
|
||||
>
|
||||
{isLoading.features ? (
|
||||
<>
|
||||
<RefreshCw className="h-4 w-4 animate-spin" />
|
||||
Saving...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Save className="h-4 w-4" />
|
||||
Save
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
)}
|
||||
{renderSaveButton("features")}
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="flex items-center justify-between rounded-lg border p-4">
|
||||
<div className="space-y-0.5">
|
||||
<Label className="text-base">Maintenance Mode</Label>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Temporarily disable access to the application
|
||||
</p>
|
||||
</div>
|
||||
<Switch
|
||||
checked={formData?.maintenanceMode || false}
|
||||
onCheckedChange={(checked) => handleInputChange("maintenanceMode", checked)}
|
||||
/>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="flex items-center justify-between rounded-lg border p-4">
|
||||
<div className="space-y-0.5">
|
||||
<Label className="text-base">Disable Admin Registration</Label>
|
||||
<p className="text-sm text-muted-foreground">Prevent creation of new admin accounts</p>
|
||||
</div>
|
||||
<Switch
|
||||
checked={formData.disableAdminRegistration}
|
||||
onCheckedChange={(checked) => handleInputChange("disableAdminRegistration", checked)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between rounded-lg border p-4">
|
||||
<div className="space-y-0.5">
|
||||
<Label className="text-base">Allow Registration</Label>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Allow new users to create accounts
|
||||
</p>
|
||||
</div>
|
||||
<Switch
|
||||
checked={formData?.allowRegistration || false}
|
||||
onCheckedChange={(checked) => handleInputChange("allowRegistration", checked)}
|
||||
/>
|
||||
<div className="flex items-center justify-between rounded-lg border p-4">
|
||||
<div className="space-y-0.5">
|
||||
<Label className="text-base">Disable Schema Mutations</Label>
|
||||
<p className="text-sm text-muted-foreground">Block create, update, and delete operations on schemas</p>
|
||||
</div>
|
||||
<Switch
|
||||
checked={formData.disableSchemaMutations}
|
||||
onCheckedChange={(checked) => handleInputChange("disableSchemaMutations", checked)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between rounded-lg border p-4">
|
||||
<div className="space-y-0.5">
|
||||
<Label className="text-base">Email Verification</Label>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Require email verification for new accounts
|
||||
</p>
|
||||
</div>
|
||||
<Switch
|
||||
checked={formData?.emailVerificationRequired || false}
|
||||
onCheckedChange={(checked) => handleInputChange("emailVerificationRequired", checked)}
|
||||
<div className="flex items-center justify-between rounded-lg border p-4">
|
||||
<div className="space-y-0.5">
|
||||
<Label className="text-base">Email Verification Required</Label>
|
||||
<p className="text-sm text-muted-foreground">Require email verification for new accounts</p>
|
||||
</div>
|
||||
<Switch
|
||||
checked={formData.emailVerificationRequired}
|
||||
onCheckedChange={(checked) => handleInputChange("emailVerificationRequired", checked)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between rounded-lg border p-4">
|
||||
<div className="space-y-0.5">
|
||||
<Label className="text-base">JWT Set Issuer</Label>
|
||||
<p className="text-sm text-muted-foreground">Include issuer claim when signing JWTs</p>
|
||||
</div>
|
||||
<Switch
|
||||
checked={formData.jwtEnableSetIssuer}
|
||||
onCheckedChange={(checked) => handleInputChange("jwtEnableSetIssuer", checked)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between rounded-lg border p-4">
|
||||
<div className="space-y-0.5">
|
||||
<Label className="text-base">JWT Set Audience</Label>
|
||||
<p className="text-sm text-muted-foreground">Include audience claim when signing JWTs</p>
|
||||
</div>
|
||||
<Switch
|
||||
checked={formData.jwtEnableSetAudience}
|
||||
onCheckedChange={(checked) => handleInputChange("jwtEnableSetAudience", checked)}
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<CardTitle>SMTP</CardTitle>
|
||||
<CardDescription>Outbound email delivery configuration</CardDescription>
|
||||
</div>
|
||||
{renderSaveButton("smtp")}
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-6">
|
||||
<div className="grid gap-4 sm:grid-cols-2">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="smtp-host">Host</Label>
|
||||
<Input
|
||||
id="smtp-host"
|
||||
value={formData.smtp.host}
|
||||
onChange={(e) => handleSmtpChange("host", e.target.value)}
|
||||
placeholder="smtp.example.com"
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="smtp-port">Port</Label>
|
||||
<Input
|
||||
id="smtp-port"
|
||||
type="number"
|
||||
value={formData.smtp.port}
|
||||
onChange={(e) => handleSmtpChange("port", Number.parseInt(e.target.value, 10) || 587)}
|
||||
min="1"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="smtp-user">Username</Label>
|
||||
<Input
|
||||
id="smtp-user"
|
||||
value={formData.smtp.user}
|
||||
onChange={(e) => handleSmtpChange("user", e.target.value)}
|
||||
placeholder="smtp-user"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="smtp-password">Password</Label>
|
||||
<Input
|
||||
id="smtp-password"
|
||||
type="password"
|
||||
value={formData.smtp.password}
|
||||
onChange={(e) => handleSmtpChange("password", e.target.value)}
|
||||
placeholder="Leave blank to keep current password"
|
||||
autoComplete="new-password"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2 sm:col-span-2">
|
||||
<Label htmlFor="smtp-from">From Address</Label>
|
||||
<Input
|
||||
id="smtp-from"
|
||||
value={formData.smtp.from}
|
||||
onChange={(e) => handleSmtpChange("from", e.target.value)}
|
||||
placeholder="noreply@example.com"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between rounded-lg border p-4">
|
||||
<div className="space-y-0.5">
|
||||
<Label className="text-base">Use TLS</Label>
|
||||
<p className="text-sm text-muted-foreground">Enable TLS for SMTP connections</p>
|
||||
</div>
|
||||
<Switch
|
||||
checked={formData.smtp.tls}
|
||||
onCheckedChange={(checked) => handleSmtpChange("tls", checked)}
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
44
lib/api.ts
44
lib/api.ts
@@ -1,9 +1,5 @@
|
||||
"use client"
|
||||
|
||||
import type { AppMode } from "./app-state"
|
||||
|
||||
|
||||
// Updated interfaces to match the API response format
|
||||
export interface FieldConstraints {
|
||||
default_value: any
|
||||
max_value: number | null
|
||||
@@ -84,31 +80,37 @@ export function getApiBaseUrl(): string {
|
||||
return override.replace(/\/+$/, "")
|
||||
}
|
||||
|
||||
const mode = process?.env?.NODE_ENV || "production"
|
||||
const port = process?.env?.MANTIS_PORT || 7070
|
||||
|
||||
if (mode === "development") {
|
||||
return `http://localhost:${port}`
|
||||
}
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
return window.location.origin
|
||||
}
|
||||
|
||||
return `http://localhost:${port}`
|
||||
const port = process?.env?.MANTIS_PORT || 7070
|
||||
return `http://127.0.0.1:${port}`
|
||||
}
|
||||
|
||||
export interface SmtpConfig {
|
||||
host: string
|
||||
port: number
|
||||
user: string
|
||||
password: string
|
||||
from: string
|
||||
tls: boolean
|
||||
}
|
||||
|
||||
export interface AppSettings {
|
||||
allowRegistration: boolean
|
||||
appName: string
|
||||
baseUrl: string
|
||||
orgName: string
|
||||
siteDomain: string
|
||||
corsAllowedOrigins: string[]
|
||||
maxFileSize: number
|
||||
logRetentionDays: number
|
||||
disableAdminRegistration: boolean
|
||||
disableSchemaMutations: boolean
|
||||
emailVerificationRequired: boolean
|
||||
maintenanceMode: boolean
|
||||
maxFileSize: Number
|
||||
mode: AppMode
|
||||
sessionTimeout: Number
|
||||
adminSessionTimeout: Number
|
||||
mantisVersion: string
|
||||
sessionTimeout: number
|
||||
adminSessionTimeout: number
|
||||
jwtEnableSetIssuer: boolean
|
||||
jwtEnableSetAudience: boolean
|
||||
smtp: SmtpConfig
|
||||
}
|
||||
|
||||
// API Response interface
|
||||
|
||||
@@ -53,6 +53,12 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
|
||||
const login = React.useCallback(async (email: string, password: string) => {
|
||||
const result = await loginWithPassword(email, password)
|
||||
const authenticated = await checkAuthSession()
|
||||
if (!authenticated) {
|
||||
throw new Error(
|
||||
"Login succeeded but the session cookie was not established. Ensure the admin UI and API share the same origin.",
|
||||
)
|
||||
}
|
||||
setUser(result.user)
|
||||
setIsAuthenticated(true)
|
||||
}, [])
|
||||
|
||||
@@ -26,6 +26,21 @@ const nextConfig = {
|
||||
// This allows dynamic routes to work in development
|
||||
...(process.env.NODE_ENV === 'production' && { output: 'export' }),
|
||||
trailingSlash: true,
|
||||
async rewrites() {
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
return []
|
||||
}
|
||||
|
||||
const port = process.env.MANTIS_PORT || '7070'
|
||||
const target = (process.env.MANTIS_PROXY_URL || `http://127.0.0.1:${port}`).replace(/\/+$/, '')
|
||||
|
||||
return [
|
||||
{
|
||||
source: '/api/:path*',
|
||||
destination: `${target}/api/:path*`,
|
||||
},
|
||||
]
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user