Skip to content

Commit 22e5cf8

Browse files
committed
feat: enforce auth on subscriptions page
1 parent c76e3f5 commit 22e5cf8

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

app/(protected)/subscriptions/page.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
TableRow,
2424
} from '@/components/ui/table'
2525
import { getListSubs } from '@/lib/api/subscription'
26+
import { Verify } from '@/lib/firebase/firebase'
2627
import Link from 'next/link'
2728

2829
export default async function Subscriptions({
@@ -38,14 +39,24 @@ export default async function Subscriptions({
3839
const skip = Number(sp?.skip ?? 0)
3940
const limit = Number(sp?.limit ?? 20)
4041
const library_id = sp?.library_id
41-
const res = await getListSubs({
42-
sort_by: 'created_at',
43-
sort_in: 'desc',
44-
limit: limit,
45-
skip: skip,
46-
...(library_id ? { library_id } : {}),
42+
43+
const headers = await Verify({
44+
from: '/borrows',
4745
})
4846

47+
const res = await getListSubs(
48+
{
49+
sort_by: 'created_at',
50+
sort_in: 'desc',
51+
limit: limit,
52+
skip: skip,
53+
...(library_id ? { library_id } : {}),
54+
},
55+
{
56+
headers,
57+
}
58+
)
59+
4960
if ('error' in res) {
5061
console.log(res)
5162
return <div>{JSON.stringify(res.message)}</div>

lib/api/subscription.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ type GetListSubsQuery = QueryParams<
1313
type GetListSubsResponse = Promise<ResList<Subscription>>
1414

1515
export const getListSubs = async (
16-
query: GetListSubsQuery
16+
query: GetListSubsQuery,
17+
init?: RequestInit
1718
): GetListSubsResponse => {
1819
const url = new URL(SUBSCRIPTIONS_URL)
1920
Object.entries(query).forEach(([key, value]) => {
@@ -22,7 +23,11 @@ export const getListSubs = async (
2223
}
2324
})
2425
try {
25-
const response = await fetch(url.toString())
26+
const response = await fetch(url.toString(), init)
27+
if (!response.ok) {
28+
const e = await response.json()
29+
throw e
30+
}
2631
return response.json()
2732
} catch (error) {
2833
return {

0 commit comments

Comments
 (0)