diff --git a/app/companies/[slug]/DeleteUserButton.tsx b/app/companies/[slug]/DeleteUserButton.tsx index baa9fa29..0c081a91 100644 --- a/app/companies/[slug]/DeleteUserButton.tsx +++ b/app/companies/[slug]/DeleteUserButton.tsx @@ -7,20 +7,22 @@ import { deleteUser } from "@/lib/crud/users" import { User } from "@prisma/client" import { Button, Dialog, Flex, Spinner } from "@radix-ui/themes" -import { useSession } from "next-auth/react" +import { signOut, useSession } from "next-auth/react" import React, { useState, useTransition } from "react" export const DeleteUserButton = ({ user }: { user: Pick }) => { + const { data: session } = useSession() const [openState, setOpenState] = useState(false) const [isPending, startTransition] = useTransition() const [serverMessage, setServerMessage] = useState("") - const { data: session } = useSession() - const handleDelete = async () => { startTransition(async () => { const { status, message } = await deleteUser(user.id, window.location.pathname) if (status === "success") { + if (user.id === session?.user.id) { + await signOut({ callbackUrl: "/" }) + } setOpenState(false) } else { setServerMessage(message || "Server error") @@ -31,16 +33,7 @@ export const DeleteUserButton = ({ user }: { user: Pick }) return ( - + Are you sure? diff --git a/components/DeleteCompany.tsx b/components/DeleteCompany.tsx index 0c7af257..6b66bc8c 100644 --- a/components/DeleteCompany.tsx +++ b/components/DeleteCompany.tsx @@ -9,6 +9,7 @@ import { FormInModal } from "./forms/FormInModal" import { GenericFormModal } from "./modals/GenericFormModal" import { Button, Spinner, Text, TextField } from "@radix-ui/themes" +import { signOut } from "next-auth/react" import { useCallback } from "react" interface DeleteCompanyFormProps { @@ -18,8 +19,13 @@ interface DeleteCompanyFormProps { } const DeleteCompanyForm = ({ close, name, id }: DeleteCompanyFormProps) => { - const deleteCompanyWithName = async (prevState: FormPassBackState, formData: FormData) => - deleteCompany(prevState, formData, id, name) + const deleteCompanyWithName = async (prevState: FormPassBackState, formData: FormData) => { + const res = await deleteCompany(prevState, formData, id, name) + if (res.status === "success") { + await signOut({ callbackUrl: "/" }) + } + return res + } return ( { const res = await deleteStudent(prevState, formData, id) if (res.status === "success") { - signOut({ callbackUrl: "/" }) + await signOut({ callbackUrl: "/" }) } return res diff --git a/lib/crud/companies.ts b/lib/crud/companies.ts index cf4361f7..4c4dc895 100644 --- a/lib/crud/companies.ts +++ b/lib/crud/companies.ts @@ -1,6 +1,7 @@ "use server" import { getCompanyLink } from "@/app/companies/getCompanyLink" +import { auth } from "@/auth" import { deleteFile } from "@/lib/files/deleteFile" import { updateUpload } from "@/lib/files/updateUpload" import { adminOnlyAction, companyOnlyAction } from "@/lib/rbac" @@ -221,6 +222,7 @@ export const updateCompany = companyOnlyAction( export const deleteCompany = companyOnlyAction( async (_: FormPassBackState, formData: FormData, companyId: number, name: string): Promise => { + const session = await auth() if (!name) return { message: "Server error: company name is null.", status: "error" } const enteredName = formData.get("name")?.toString().trim() @@ -256,6 +258,10 @@ export const deleteCompany = companyOnlyAction( return { message: "A database error occurred. Please try again later.", status: "error" } } - redirect("/companies") + if (session!.user.role === Role.ADMIN) { + redirect("/companies") + } else { + return { message: "Company deleted successfully.", status: "success" } + } }, ) diff --git a/lib/crud/users.ts b/lib/crud/users.ts index 0ec265ef..001491ed 100644 --- a/lib/crud/users.ts +++ b/lib/crud/users.ts @@ -23,10 +23,6 @@ export const allowedToDeleteUser = async (userId: string): Promise