Skip to content

Commit 68021f8

Browse files
committed
fix: private songs not being accessible by the uploader
1 parent 1a71c06 commit 68021f8

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

server/src/song/song.service.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,7 @@ export class SongService {
260260
publicId: string,
261261
user: UserDocument | null,
262262
): Promise<SongViewDto> {
263-
const foundSong = await this.songModel
264-
.findOne({ publicId: publicId })
265-
.populate('uploader', 'username profileImage -_id');
263+
const foundSong = await this.songModel.findOne({ publicId: publicId });
266264

267265
if (!foundSong) {
268266
throw new HttpException('Song not found', HttpStatus.NOT_FOUND);
@@ -282,7 +280,12 @@ export class SongService {
282280
foundSong.playCount++;
283281
await foundSong.save();
284282

285-
return SongViewDto.fromSongDocument(foundSong);
283+
const populatedSong = await foundSong.populate(
284+
'uploader',
285+
'username profileImage -_id',
286+
);
287+
288+
return SongViewDto.fromSongDocument(populatedSong);
286289
}
287290

288291
// TODO: service should not handle HTTP -> https://www.reddit.com/r/node/comments/uoicw1/should_i_return_status_code_from_service_layer/

web/src/app/(content)/song/[id]/page.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { SongViewDtoType } from '@shared/validation/song/dto/types';
22
import type { Metadata } from 'next';
3+
import { cookies } from 'next/headers';
34

45
import axios from '@web/src/lib/axios';
56
import { SongPage } from '@web/src/modules/song/components/SongPage';
@@ -16,9 +17,21 @@ export async function generateMetadata({
1617
let song;
1718
const publicUrl = process.env.NEXT_PUBLIC_URL;
1819

20+
const cookieStore = await cookies();
21+
const token = cookieStore.get('token')?.value || null;
22+
23+
const headers: Record<string, string> = {};
24+
25+
if (token) {
26+
headers.Authorization = `Bearer ${token}`;
27+
}
28+
1929
try {
20-
const response = await axios.get<SongViewDtoType>(`/song/${params.id}`);
21-
song = response.data;
30+
const response = await axios.get<SongViewDtoType>(`/song/${params.id}`, {
31+
headers,
32+
});
33+
34+
song = await response.data;
2235
} catch {
2336
return {
2437
title: 'Unknown song!',

web/src/modules/song/components/SongPage.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { SongPreviewDto } from '@shared/validation/song/dto/SongPreview.dto';
22
import { SongViewDtoType } from '@shared/validation/song/dto/types';
3+
import { cookies } from 'next/headers';
34
import Image from 'next/image';
45

56
import axios from '@web/src/lib/axios';
@@ -21,8 +22,21 @@ import { formatTimeAgo } from '../../shared/util/format';
2122
export async function SongPage({ id }: { id: string }) {
2223
let song;
2324

25+
// get 'token' cookie from headers
26+
const cookieStore = await cookies();
27+
const token = cookieStore.get('token')?.value || null;
28+
29+
const headers: Record<string, string> = {};
30+
31+
if (token) {
32+
headers.Authorization = `Bearer ${token}`;
33+
}
34+
2435
try {
25-
const response = await axios.get<SongViewDtoType>(`/song/${id}`);
36+
const response = await axios.get<SongViewDtoType>(`/song/${id}`, {
37+
headers,
38+
});
39+
2640
song = await response.data;
2741
} catch {
2842
return <ErrorBox message='An error occurred while retrieving the song' />;

0 commit comments

Comments
 (0)