-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAuthSettings.tsx
More file actions
100 lines (92 loc) · 2.76 KB
/
AuthSettings.tsx
File metadata and controls
100 lines (92 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { useEffect, useState } from "react";
import { useQuery, useMutation } from "urql";
import { toast } from "sonner";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
const SETTINGS_QUERY = `
query AuthSettings {
allSettings(filter: { key: { startsWith: "auth." } }) {
nodes { key value }
}
}
`;
const SAVE_SETTING_MUTATION = `
mutation SaveSetting($pKey: String!, $pValue: JSON!) {
saveSetting(input: { pKey: $pKey, pValue: $pValue }) {
setting { key value }
}
}
`;
export function AuthSettings() {
const [result, reexecute] = useQuery({ query: SETTINGS_QUERY });
const [, saveSetting] = useMutation(SAVE_SETTING_MUTATION);
const [selfRegistration, setSelfRegistration] = useState(false);
const [saving, setSaving] = useState(false);
const settings: Record<string, any> = {};
for (const node of result.data?.allSettings?.nodes || []) {
settings[node.key] = node.value;
}
useEffect(() => {
if (result.data) {
setSelfRegistration(settings["auth.selfRegistration"] === true);
}
}, [result.data]);
async function handleToggle(checked: boolean) {
setSelfRegistration(checked);
setSaving(true);
try {
const res = await saveSetting({ pKey: "auth.selfRegistration", pValue: checked });
if (res.error) {
toast.error(res.error.message);
setSelfRegistration(!checked);
return;
}
reexecute({ requestPolicy: "network-only" });
toast.success("Setting saved.");
} catch {
toast.error("Failed to save setting.");
setSelfRegistration(!checked);
} finally {
setSaving(false);
}
}
return (
<div className="space-y-6">
<div>
<h2 className="text-2xl font-bold">Auth Settings</h2>
<p className="text-muted-foreground">
Configure authentication behavior
</p>
</div>
<Card>
<CardHeader>
<CardTitle>Self Registration</CardTitle>
<CardDescription>
Allow new users to create accounts via the registration page.
When disabled, only admins can create user accounts.
</CardDescription>
</CardHeader>
<CardContent>
<div className="flex items-center gap-3">
<Switch
id="self-registration"
checked={selfRegistration}
onCheckedChange={handleToggle}
disabled={saving || result.fetching}
/>
<Label htmlFor="self-registration">
{selfRegistration ? "Enabled" : "Disabled"}
</Label>
</div>
</CardContent>
</Card>
</div>
);
}