import { useState, type FormEvent } from "react"; import { Link } from "react-router-dom"; import { api, type AuthentikGroup, type AuthentikInfo, type AuthentikUserMade } from "../api/client"; import CopyButton from "./CopyButton"; import { ErrorNotice } from "./Notice"; import { confirmDialog } from "../utils/dialogs"; import { readableError } from "../utils/errors"; import { t } from "../i18n"; const EMPTY = { username: "", name: "", email: "" }; /** * Making a user in Authentik, for an administrator: a name, an email if there is one, the groups, and out comes a one-time link for them to choose their own password with. The app never * sees or sets a password, and the link is shown here once — it is not kept anywhere. */ export default function AuthentikNewUser({ a, onChanged }: { a: AuthentikInfo; onChanged: () => void }) { const [open, setOpen] = useState(false); const [fields, setFields] = useState(EMPTY); const [groups, setGroups] = useState(null); const [groupsError, setGroupsError] = useState(null); const [picked, setPicked] = useState([]); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [made, setMade] = useState(null); if (!a.canCreateUsers) { return (
To make and manage users from here, give this Authentik a token that may add, change and delete them: {t("Integrations")} → edit → Token for making and managing users.
); } async function show() { setOpen(true); setMade(null); setError(null); setGroupsError(null); try { setGroups((await api.authentik.groups(a.id)).groups); } catch (err) { setGroups([]); setGroupsError(readableError(err)); } } function close() { setOpen(false); setFields(EMPTY); setPicked([]); setMade(null); setError(null); } async function submit(e: FormEvent) { e.preventDefault(); if (busy) return; setBusy(true); setError(null); try { setMade(await api.authentik.createUser(a.id, { ...fields, groups: picked })); setFields(EMPTY); setPicked([]); } catch (err) { setError(readableError(err)); } finally { setBusy(false); } } async function stopAllowing() { if (!(await confirmDialog({ title: t("Stop managing users"), message: `Remove the token for making and managing users from "${a.name}"? Reading it carries on. You can add one again under Integrations.`, confirmLabel: t("Remove"), danger: true }))) return; try { await api.authentik.removeWriteToken(a.id); close(); onChanged(); } catch (err) { setError(readableError(err)); } } const toggle = (id: string) => setPicked((p) => (p.includes(id) ? p.filter((x) => x !== id) : [...p, id])); return (

{t("New user")}

{!open && (
)} {open && made && (
{made.user.username} was made in {a.name}.
{made.link ? ( <>
e.currentTarget.select()} />
Send them this link. They open it and choose their own password; it works once. It is shown here only and kept nowhere, so copy it now. It points at the address this app reaches Authentik at, so the Authentik address in Integrations should be the one people use.
) : (
The user exists, but no link could be made: {made.linkError ?? "no answer"}. Make one in Authentik: Directory → Users → {made.user.username} → Create recovery link.
)}
)} {open && !made && (
setFields({ ...fields, username: e.target.value })} />
setFields({ ...fields, name: e.target.value })} />
setFields({ ...fields, email: e.target.value })} />
{t("Groups")}
{groups === null &&
{t("Reading the groups…")}
} {groupsError &&
The groups could not be read: {groupsError}. The user can still be made, without groups.
} {groups && groups.length === 0 && !groupsError &&
{t("There are no groups.")}
} {groups && groups.length > 0 && (
{groups.map((g) => ( ))}
)}
They choose their own password with the link; none is set here.
)}
); }