Skip to content

Commit

Permalink
fix delete user
Browse files Browse the repository at this point in the history
  • Loading branch information
potts99 committed Mar 21, 2024
1 parent b96f76b commit d8914ad
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 53 deletions.
6 changes: 5 additions & 1 deletion apps/api/src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function authRoutes(fastify: FastifyInstance) {
if (bearer) {
const token = checkToken(bearer);
if (token) {
const requester = await checkSession(token);
const requester = await checkSession(bearer);

if (!requester?.isAdmin) {
reply.code(401).send({
Expand Down Expand Up @@ -396,6 +396,10 @@ export function authRoutes(fastify: FastifyInstance) {
if (token) {
const { id } = request.params as { id: string };

await prisma.notes.deleteMany({ where: { userId: id } });
await prisma.session.deleteMany({ where: { userId: id } });
await prisma.notifications.deleteMany({ where: { userId: id } });

await prisma.user.delete({
where: { id },
});
Expand Down
36 changes: 0 additions & 36 deletions apps/api/src/controllers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,42 +101,6 @@ export function userRoutes(fastify: FastifyInstance) {
}
);

// Delete user

// Update user
fastify.put(
"/api/v1/user/update",

async (request: FastifyRequest, reply: FastifyReply) => {
const bearer = request.headers.authorization!.split(" ")[1];

const { name, email, admin, id }: any = request.body;

if (bearer) {
const token = checkToken(bearer);
if (token) {
await prisma.user.update({
where: { id: id },
data: {
name,
email,
isAdmin: admin,
},
});

reply.send({
success: true,
});
}
} else {
reply.send({
success: false,
message: "No token provided",
});
}
}
);

// Mark Notification as read
fastify.get(
"/api/v1/user/notifcation/:id",
Expand Down
29 changes: 13 additions & 16 deletions apps/client/pages/admin/users/internal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,22 +205,19 @@ export default function UserAuthPanel() {
);

async function deleteUser(id) {
if (confirm("Are you sure you want to delete this user?")) {
try {
await fetch(`/api/v1/auth/user/${id}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
})
.then((response) => response.json())
.then(() => {
refetch;
});
} catch (error) {
console.log(error);
}
try {
await fetch(`/api/v1/auth/user/${id}`, {
method: "DELETE",
headers: {
Authorization: `Bearer ${token}`,
},
})
.then((response) => response.json())
.then(() => {
refetch();
});
} catch (error) {
console.log(error);
}
}

Expand Down

0 comments on commit d8914ad

Please sign in to comment.