Skip to content

Commit

Permalink
Merge pull request #1298 from paahaad/fix/updatecontent-api
Browse files Browse the repository at this point in the history
Added authentication to content update apis.
  • Loading branch information
hkirat authored Sep 23, 2024
2 parents 13f7207 + 83f27d1 commit 57de62b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 56 deletions.
39 changes: 24 additions & 15 deletions src/app/api/admin/contentmetadata/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@ import db from '@/db';
import { NextRequest, NextResponse } from 'next/server';

export const POST = async (req: NextRequest) => {
const {
updates,
contentId,
}: {
updates: any;
contentId: number;
} = await req.json();

await db.videoMetadata.update({
where: {
try {
const {
updates,
contentId,
},
data: updates,
});

return NextResponse.json({}, { status: 200 });
adminPassword,
}: {
updates: any;
contentId: number;
adminPassword: string;
} = await req.json();

if (adminPassword !== process.env.ADMIN_SECRET) {
return NextResponse.json({}, { status: 403 });
}
await db.videoMetadata.update({
where: {
contentId,
},
data: updates,
});

return NextResponse.json({}, { status: 200 });
} catch (err) {
return NextResponse.json({}, { status: 500 });
}
};
90 changes: 50 additions & 40 deletions src/app/api/admin/updatecontent/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,57 @@ import db from '@/db';
import { NextRequest, NextResponse } from 'next/server';

export const POST = async (req: NextRequest) => {
const {
updates,
contentId,
}: {
updates: any;
contentId: number;
} = await req.json();

await db.videoMetadata.update({
where: {
try {
const {
updates,
contentId,
},
data: {
video_360p_1: updates.video_360p,
video_360p_2: updates.video_360p,
video_360p_3: updates.video_360p,
video_360p_4: updates.video_360p,
video_720p_1: updates.video_720p,
video_720p_2: updates.video_720p,
video_720p_3: updates.video_720p,
video_720p_4: updates.video_720p,
video_1080p_1: updates.video_1080p,
video_1080p_2: updates.video_1080p,
video_1080p_3: updates.video_1080p,
video_1080p_4: updates.video_1080p,
/// mp4s
adminPassword,
}: {
updates: any;
contentId: number;
adminPassword: string;
} = await req.json();

video_1080p_mp4_1: updates.video_1080p_mp4,
video_1080p_mp4_2: updates.video_1080p_mp4,
video_1080p_mp4_3: updates.video_1080p_mp4,
video_1080p_mp4_4: updates.video_1080p_mp4,
video_720p_mp4_1: updates.video_720p_mp4,
video_720p_mp4_2: updates.video_720p_mp4,
video_720p_mp4_3: updates.video_720p_mp4,
video_720p_mp4_4: updates.video_720p_mp4,
video_360p_mp4_1: updates.video_360p_mp4,
video_360p_mp4_2: updates.video_360p_mp4,
video_360p_mp4_3: updates.video_360p_mp4,
video_360p_mp4_4: updates.video_360p_mp4,
},
});
if (adminPassword !== process.env.ADMIN_SECRET) {
return NextResponse.json({}, { status: 403 });
}

return NextResponse.json({}, { status: 200 });
await db.videoMetadata.update({
where: {
contentId,
},
data: {
video_360p_1: updates.video_360p,
video_360p_2: updates.video_360p,
video_360p_3: updates.video_360p,
video_360p_4: updates.video_360p,
video_720p_1: updates.video_720p,
video_720p_2: updates.video_720p,
video_720p_3: updates.video_720p,
video_720p_4: updates.video_720p,
video_1080p_1: updates.video_1080p,
video_1080p_2: updates.video_1080p,
video_1080p_3: updates.video_1080p,
video_1080p_4: updates.video_1080p,
/// mp4s

video_1080p_mp4_1: updates.video_1080p_mp4,
video_1080p_mp4_2: updates.video_1080p_mp4,
video_1080p_mp4_3: updates.video_1080p_mp4,
video_1080p_mp4_4: updates.video_1080p_mp4,
video_720p_mp4_1: updates.video_720p_mp4,
video_720p_mp4_2: updates.video_720p_mp4,
video_720p_mp4_3: updates.video_720p_mp4,
video_720p_mp4_4: updates.video_720p_mp4,
video_360p_mp4_1: updates.video_360p_mp4,
video_360p_mp4_2: updates.video_360p_mp4,
video_360p_mp4_3: updates.video_360p_mp4,
video_360p_mp4_4: updates.video_360p_mp4,
},
});

return NextResponse.json({}, { status: 200 });
} catch (err) {
return NextResponse.json({}, { status: 500 });
}
};
23 changes: 22 additions & 1 deletion src/components/admin/UpdateVideoClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const UpdateVideoClient = ({
const [pdfLink, setPdfLink] = useState('');
const [vttLink, setVttLink] = useState('');

const [adminPassword, setAdminPassword] = useState('');
return (
<div className='max-w-7xl mx-auto px-4'>

Expand Down Expand Up @@ -108,10 +109,17 @@ export const UpdateVideoClient = ({
}}
placeholder={'m3u8 360p'}
/>
<Label className="mt-4">Admin Password</Label>
<Input
type="text"
placeholder="Admin password"
onChange={(e) => setAdminPassword(e.target.value)}
/>
<Button
className="my-4 rounded p-2 font-bold text-white w-full lg:w-[20%]"
className="my-4 w-full rounded p-2 font-bold text-white lg:w-[20%]"
onClick={async () => {
await axios.post('/api/admin/updatecontent', {
adminPassword,
contentId: content.id,
updates: {
video_360p: link360,
Expand Down Expand Up @@ -152,10 +160,16 @@ export const UpdateVideoClient = ({
}}
placeholder={'pdf link'}
/>
<Input
type="text"
placeholder="Admin password"
onChange={(e) => setAdminPassword(e.target.value)}
/>
<Button
className="my-4 rounded p-2 font-bold text-white w-full lg:w-[20%]"
onClick={async () => {
await axios.post('/api/admin/contentmetadata', {
adminPassword,
contentId: content.id,
updates: {
slides: pdfLink,
Expand Down Expand Up @@ -190,10 +204,17 @@ export const UpdateVideoClient = ({
}}
placeholder={'vtt link'}
/>
<Input
type="text"
placeholder="Admin password"
onChange={(e) => setAdminPassword(e.target.value)}
className="my-3"
/>
<Button
className="my-4 rounded p-2 font-bold text-white w-full lg:w-[20%]"
onClick={async () => {
await axios.post('/api/admin/contentmetadata', {
adminPassword,
contentId: content.id,
updates: {
subtitles: vttLink,
Expand Down

0 comments on commit 57de62b

Please sign in to comment.