File tree Expand file tree Collapse file tree 3 files changed +37
-7
lines changed Expand file tree Collapse file tree 3 files changed +37
-7
lines changed Original file line number Diff line number Diff line change @@ -260,9 +260,7 @@ export class SongService {
260
260
publicId : string ,
261
261
user : UserDocument | null ,
262
262
) : 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 } ) ;
266
264
267
265
if ( ! foundSong ) {
268
266
throw new HttpException ( 'Song not found' , HttpStatus . NOT_FOUND ) ;
@@ -282,7 +280,12 @@ export class SongService {
282
280
foundSong . playCount ++ ;
283
281
await foundSong . save ( ) ;
284
282
285
- return SongViewDto . fromSongDocument ( foundSong ) ;
283
+ const populatedSong = await foundSong . populate (
284
+ 'uploader' ,
285
+ 'username profileImage -_id' ,
286
+ ) ;
287
+
288
+ return SongViewDto . fromSongDocument ( populatedSong ) ;
286
289
}
287
290
288
291
// TODO: service should not handle HTTP -> https://www.reddit.com/r/node/comments/uoicw1/should_i_return_status_code_from_service_layer/
Original file line number Diff line number Diff line change 1
1
import { SongViewDtoType } from '@shared/validation/song/dto/types' ;
2
2
import type { Metadata } from 'next' ;
3
+ import { cookies } from 'next/headers' ;
3
4
4
5
import axios from '@web/src/lib/axios' ;
5
6
import { SongPage } from '@web/src/modules/song/components/SongPage' ;
@@ -16,9 +17,21 @@ export async function generateMetadata({
16
17
let song ;
17
18
const publicUrl = process . env . NEXT_PUBLIC_URL ;
18
19
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
+
19
29
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 ;
22
35
} catch {
23
36
return {
24
37
title : 'Unknown song!' ,
Original file line number Diff line number Diff line change 1
1
import { SongPreviewDto } from '@shared/validation/song/dto/SongPreview.dto' ;
2
2
import { SongViewDtoType } from '@shared/validation/song/dto/types' ;
3
+ import { cookies } from 'next/headers' ;
3
4
import Image from 'next/image' ;
4
5
5
6
import axios from '@web/src/lib/axios' ;
@@ -21,8 +22,21 @@ import { formatTimeAgo } from '../../shared/util/format';
21
22
export async function SongPage ( { id } : { id : string } ) {
22
23
let song ;
23
24
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
+
24
35
try {
25
- const response = await axios . get < SongViewDtoType > ( `/song/${ id } ` ) ;
36
+ const response = await axios . get < SongViewDtoType > ( `/song/${ id } ` , {
37
+ headers,
38
+ } ) ;
39
+
26
40
song = await response . data ;
27
41
} catch {
28
42
return < ErrorBox message = 'An error occurred while retrieving the song' /> ;
You can’t perform that action at this time.
0 commit comments