Skip to content

Commit

Permalink
Merge pull request #102 from imperial/fix-logout-deleted-company-user
Browse files Browse the repository at this point in the history
Fix logout deleted company user
  • Loading branch information
matalex412 authored Sep 3, 2024
2 parents 245e9bd + b4f7b58 commit d23da08
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
19 changes: 6 additions & 13 deletions app/companies/[slug]/DeleteUserButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<User, "id" | "email"> }) => {
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")
Expand All @@ -31,16 +33,7 @@ export const DeleteUserButton = ({ user }: { user: Pick<User, "id" | "email"> })
return (
<Dialog.Root open={openState} onOpenChange={setOpenState} defaultOpen={false}>
<Dialog.Trigger>
<DeleteButton
text="Delete"
variant="outline"
disabled={session?.user.id === user.id}
title={
session?.user.id === user.id
? "You can't delete yourself - contact the admins for help."
: `Delete user ${user.email}`
}
/>
<DeleteButton text="Delete" title={`Delete user ${user.email}`} />
</Dialog.Trigger>
<DangerModalContent>
<Dialog.Title>Are you sure?</Dialog.Title>
Expand Down
10 changes: 8 additions & 2 deletions components/DeleteCompany.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 (
<FormInModal
Expand Down
2 changes: 1 addition & 1 deletion components/DeleteStudent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const DeleteStudentForm = ({ close, id }: DeleteStudentFormProps) => {
const res = await deleteStudent(prevState, formData, id)

if (res.status === "success") {
signOut({ callbackUrl: "/" })
await signOut({ callbackUrl: "/" })
}

return res
Expand Down
8 changes: 7 additions & 1 deletion lib/crud/companies.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -221,6 +222,7 @@ export const updateCompany = companyOnlyAction(

export const deleteCompany = companyOnlyAction(
async (_: FormPassBackState, formData: FormData, companyId: number, name: string): Promise<FormPassBackState> => {
const session = await auth()
if (!name) return { message: "Server error: company name is null.", status: "error" }

const enteredName = formData.get("name")?.toString().trim()
Expand Down Expand Up @@ -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" }
}
},
)
4 changes: 0 additions & 4 deletions lib/crud/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ export const allowedToDeleteUser = async (userId: string): Promise<string | unde
if (session.user.role === Role.ADMIN) {
return
}
// Users can NOT delete themselves
if (session.user.id === userId) {
return "You cannot delete yourself - contact an admin for help"
}

// Company users can delete users in the same company
if (session.user.role === Role.COMPANY) {
Expand Down

0 comments on commit d23da08

Please sign in to comment.