mirror of
https://github.com/OHTU-German-learning-website/OHTU-German-learning-website.git
synced 2026-10-12 19:13:51 +03:00
Merge pull request #515 from erzabytyci/user-tiers
make talback admin only
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { z } from "zod";
|
||||
import { withAuth } from "@/backend/middleware/withAuth";
|
||||
import sendEmail from "@/backend/mailService";
|
||||
|
||||
const talkbackSchema = z.object({
|
||||
@@ -18,37 +19,43 @@ const talkbackSchema = z.object({
|
||||
.max(2000, "Nachricht ist zu lang."),
|
||||
});
|
||||
|
||||
export async function POST(req) {
|
||||
try {
|
||||
const body = await req.json();
|
||||
const result = talkbackSchema.safeParse(body);
|
||||
export const POST = withAuth(
|
||||
async (req) => {
|
||||
try {
|
||||
const body = await req.json();
|
||||
const result = talkbackSchema.safeParse(body);
|
||||
|
||||
if (!result.success) {
|
||||
if (!result.success) {
|
||||
return Response.json(
|
||||
{
|
||||
message: result.error.issues[0]?.message || "Ungültige Eingabe.",
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const { email, subject, message } = result.data;
|
||||
|
||||
await sendEmail({
|
||||
to: email,
|
||||
subject,
|
||||
text: message,
|
||||
});
|
||||
|
||||
return Response.json({
|
||||
message: "Nachricht erfolgreich gesendet.",
|
||||
});
|
||||
} catch {
|
||||
return Response.json(
|
||||
{
|
||||
message: result.error.issues[0]?.message || "Ungültige Eingabe.",
|
||||
message: "Nachricht konnte nicht gesendet werden.",
|
||||
},
|
||||
{ status: 400 }
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
const { email, subject, message } = result.data;
|
||||
|
||||
await sendEmail({
|
||||
to: email,
|
||||
subject,
|
||||
text: message,
|
||||
});
|
||||
|
||||
return Response.json({
|
||||
message: "Nachricht erfolgreich gesendet.",
|
||||
});
|
||||
} catch {
|
||||
return Response.json(
|
||||
{
|
||||
message: "Nachricht konnte nicht gesendet werden.",
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
},
|
||||
{
|
||||
requireAuth: true,
|
||||
requireAdmin: true,
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
@@ -2,10 +2,26 @@
|
||||
|
||||
import "./talkback.css";
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect } from "react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { withBasePath } from "@/shared/utils/basePath";
|
||||
import { useIsAdmin } from "@/context/user.context";
|
||||
|
||||
export default function TalkbackPage() {
|
||||
const isAdmin = useIsAdmin();
|
||||
const router = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
if (isAdmin === false) {
|
||||
router.replace(withBasePath("/"));
|
||||
}
|
||||
}, [isAdmin, router]);
|
||||
|
||||
if (isAdmin !== true) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const defaultSubject = "Rückmeldung von GRADESA 2.0";
|
||||
|
||||
const [email, setEmail] = useState("");
|
||||
|
||||
Reference in New Issue
Block a user