Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

add adduser

Bill Yang8ec6f084a5e2
diff --git a/client/src/app/settings/users/AddUser.tsx b/client/src/app/settings/users/AddUser.tsx
index 260cab7f0..ef45fb552 100644
--- a/client/src/app/settings/users/AddUser.tsx
+++ b/client/src/app/settings/users/AddUser.tsx
@@ -19,7 +19,7 @@ import { Label } from "../../../components/ui/label";
import { Input } from "../../../components/ui/input";
import { Switch } from "../../../components/ui/switch";
-export function AddUser() {
+export function AddUser({ refetch }: { refetch: () => void }) {
const [open, setOpen] = useState(false);
const [name, setName] = useState("");
const [email, setEmail] = useState("");
@@ -35,7 +35,7 @@ export function AddUser() {
setError("");
if (password !== confirmPassword) {
- setError("New password and confirm password do not match");
+ setError("password and confirm password do not match");
return;
}
try {
@@ -52,6 +52,7 @@ export function AddUser() {
}
setOpen(false);
+ refetch();
} catch (error) {
setError(String(error));
}
diff --git a/client/src/app/settings/users/DeleteUser.tsx b/client/src/app/settings/users/DeleteUser.tsx
new file mode 100644
index 000000000..52e11edc9
--- /dev/null
+++ b/client/src/app/settings/users/DeleteUser.tsx
@@ -0,0 +1,36 @@
+import { useState } from "react";
+import { ConfirmationModal } from "../../../components/ConfirmationModal";
+import { Button } from "../../../components/ui/button";
+import { UserWithRole } from "better-auth/plugins/admin";
+import { authClient } from "../../../lib/auth";
+
+export function DeleteUser({
+ user,
+ refetch,
+}: {
+ user: UserWithRole;
+ refetch: () => void;
+}) {
+ const [isOpen, setIsOpen] = useState(false);
+
+ return (
+ <ConfirmationModal
+ title="Delete User"
+ description="Are you sure you want to delete this user?"
+ isOpen={isOpen}
+ setIsOpen={setIsOpen}
+ onConfirm={async () => {
+ await authClient.admin.removeUser({ userId: user.id });
+ refetch();
+ }}
+ primaryAction={{
+ variant: "destructive",
+ children: "Delete",
+ }}
+ >
+ <Button variant={"destructive"} size={"sm"}>
+ Delete
+ </Button>
+ </ConfirmationModal>
+ );
+}
diff --git a/client/src/app/settings/users/Users.tsx b/client/src/app/settings/users/Users.tsx
index abb06f287..2977ed869 100644
--- a/client/src/app/settings/users/Users.tsx
+++ b/client/src/app/settings/users/Users.tsx
@@ -1,7 +1,6 @@
"use client";
-
+import { useQuery } from "@tanstack/react-query";
import { DateTime } from "luxon";
-import { Button } from "../../../components/ui/button";
import {
Card,
CardContent,
@@ -16,11 +15,22 @@ import {
TableHeader,
TableRow,
} from "../../../components/ui/table";
-import { useListUsers } from "../../../hooks/api";
+import { authClient } from "../../../lib/auth";
import { AddUser } from "./AddUser";
+import { DeleteUser } from "./DeleteUser";
export function Users() {
- const { data: users, refetch } = useListUsers();
+ const { data: users, refetch } = useQuery({
+ queryKey: ["users"],
+ queryFn: async () => {
+ const users = await authClient.admin.listUsers({ query: { limit: 100 } });
+ return users;
+ },
+ });
+
+ if (users?.error) {
+ return null;
+ }
return (
<div className="flex flex-col gap-4">
@@ -28,7 +38,7 @@ export function Users() {
<CardHeader>
<CardTitle className="text-xl flex justify-between items-center">
Users
- <AddUser />
+ <AddUser refetch={refetch} />
</CardTitle>
</CardHeader>
<CardContent className="flex flex-col gap-4">
@@ -44,35 +54,24 @@ export function Users() {
</TableRow>
</TableHeader>
<TableBody>
- {users?.data?.map((user) => (
+ {users?.data?.users?.map((user) => (
<TableRow key={user.id}>
<TableCell>{user.name}</TableCell>
<TableCell>{user.role || "admin"}</TableCell>
<TableCell>{user.email}</TableCell>
<TableCell>
- {DateTime.fromISO(user.createdAt).toLocaleString()}
+ {DateTime.fromJSDate(user.createdAt).toLocaleString()}
</TableCell>
<TableCell>
{user.name !== "admin" && (
- <Button variant={"destructive"} size={"sm"}>
- Delete
- </Button>
+ <DeleteUser user={user} refetch={refetch} />
)}
</TableCell>
</TableRow>
))}
- {/* <TableRow>
- <TableCell>Paid</TableCell>
- <TableCell>Credit Card</TableCell>
- </TableRow> */}
</TableBody>
</Table>
</CardContent>
- {/* <CardFooter className="flex justify-end">
- <Button variant={"accent"} disabled={!hasChanges}>
- Save Changes
- </Button>
- </CardFooter> */}
</Card>
</div>
);
diff --git a/client/src/components/ConfirmationModal.tsx b/client/src/components/ConfirmationModal.tsx
new file mode 100644
index 000000000..c36efb90d
--- /dev/null
+++ b/client/src/components/ConfirmationModal.tsx
@@ -0,0 +1,78 @@
+import React, { useState } from "react";
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+ DialogTrigger,
+} from "./ui/dialog";
+import { Button, ButtonProps } from "./ui/button";
+import { Alert, AlertDescription, AlertTitle } from "./ui/alert";
+import { AlertCircle } from "lucide-react";
+
+export const ConfirmationModal: React.FC<{
+ title: React.ReactNode;
+ description: React.ReactNode;
+ children: React.ReactNode;
+ isOpen: boolean;
+ setIsOpen: (b: boolean) => void;
+ onConfirm: () => void;
+ primaryAction?: ButtonProps;
+}> = ({
+ title,
+ description,
+ children,
+ isOpen,
+ onConfirm,
+ setIsOpen,
+ primaryAction,
+}) => {
+ const [error, setError] = useState<string | null>(null);
+
+ const onSubmit = async () => {
+ setError(null);
+ try {
+ await onConfirm();
+ setIsOpen(false);
+ } catch (error) {
+ setError(String(error));
+ }
+ };
+
+ return (
+ <div>
+ <Dialog open={isOpen} onOpenChange={(e) => setIsOpen(e)}>
+ <DialogTrigger asChild>{children}</DialogTrigger>
+ <DialogContent className="max-w-lg">
+ <DialogHeader>
+ <DialogTitle>{title}</DialogTitle>
+ <DialogDescription>{description}</DialogDescription>
+ </DialogHeader>
+ {error && (
+ <Alert variant="destructive">
+ <AlertCircle className="h-4 w-4" />
+ <AlertTitle>Error</AlertTitle>
+ <AlertDescription>{error}</AlertDescription>
+ </Alert>
+ )}
+ <DialogFooter>
+ <Button
+ type="submit"
+ onClick={() => setIsOpen(false)}
+ variant={"ghost"}
+ >
+ Cancel
+ </Button>
+ <Button
+ type="submit"
+ onClick={onSubmit}
+ {...primaryAction}
+ ></Button>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ </div>
+ );
+};
diff --git a/client/src/hooks/api.ts b/client/src/hooks/api.ts
index 19bbb638a..441efd080 100644
--- a/client/src/hooks/api.ts
+++ b/client/src/hooks/api.ts
@@ -3,9 +3,9 @@ import {
useQuery,
UseQueryResult,
} from "@tanstack/react-query";
+import { BACKEND_URL } from "../lib/const";
import { useStore } from "../lib/store";
import { authedFetch, getStartAndEndDate } from "./utils";
-import { BACKEND_URL } from "../lib/const";
export type APIResponse<T> = {
data: T;
@@ -173,18 +173,3 @@ export function deleteSite(siteId: string) {
method: "POST",
});
}
-
-export type ListUsersResponse = {
- id: string;
- name: string;
- username: string;
- email: string;
- emailVerified: boolean;
- role: string;
- createdAt: string;
- updatedAt: string;
-}[];
-
-export function useListUsers() {
- return useGenericQuery<ListUsersResponse>("list-users");
-}