diff --git a/src/components/shared/ProfileInfo.tsx b/src/components/shared/ProfileInfo.tsx index 055babd..9349f03 100644 --- a/src/components/shared/ProfileInfo.tsx +++ b/src/components/shared/ProfileInfo.tsx @@ -36,9 +36,9 @@ export default function ProfileInfo() { {user && ( <>

{user?.name}

- + } )} diff --git a/src/handlers/serverUtils/user.utils.ts b/src/handlers/serverUtils/user.utils.ts index a3c732b..abdc2d8 100644 --- a/src/handlers/serverUtils/user.utils.ts +++ b/src/handlers/serverUtils/user.utils.ts @@ -15,7 +15,12 @@ export const getCurrentUserFromAPIKey = () => { }, }).then((res) => { if (res.ok) { - return res.json() + return res.json().then((user) => { + return { + ...user, + isUsingAPIKey: true, + } + }) } return null }) diff --git a/src/helpers/user.helper.ts b/src/helpers/user.helper.ts new file mode 100644 index 0000000..2d86ef3 --- /dev/null +++ b/src/helpers/user.helper.ts @@ -0,0 +1,21 @@ +import { ENV } from "@/config/environment" +import { IUser } from "@/types/user" + +export const getUserHeaders = (user: IUser, otherHeaders?: { + 'Content-Type': string +}) => { + let headers: { + 'Content-Type': string; + 'x-api-key'?: string; + 'Authorization'?: string + } = { + 'Content-Type': 'application/octet-stream', + } + if (user.isUsingAPIKey) { + headers['x-api-key'] = ENV.IMMICH_API_KEY + } + else { + headers['Authorization'] = `Bearer ${user.accessToken}` + } + return {...headers, ...otherHeaders} +} \ No newline at end of file diff --git a/src/pages/api/immich-proxy/[...path].ts b/src/pages/api/immich-proxy/[...path].ts index 791e789..c7641f7 100644 --- a/src/pages/api/immich-proxy/[...path].ts +++ b/src/pages/api/immich-proxy/[...path].ts @@ -1,6 +1,8 @@ // pages/api/proxy/[...path].ts import { ENV } from '@/config/environment'; +import { getCurrentUser } from '@/handlers/serverUtils/user.utils'; +import { getUserHeaders } from '@/helpers/user.helper'; import { NextApiRequest, NextApiResponse } from 'next' export default async function handler(req: NextApiRequest, res: NextApiResponse) { @@ -8,14 +10,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) const targetUrl = `${ENV.IMMICH_URL}/api${proxyPath}`; + const currentUser = await getCurrentUser(req); try { const response = await fetch(targetUrl, { method: req.method, - headers: { - 'x-api-key': ENV.IMMICH_API_KEY, - 'Content-Type': 'application/json', - }, - + headers: getUserHeaders(currentUser), body: req.method !== 'GET' && req.method !== 'HEAD' ? JSON.stringify(req.body) : null, }) diff --git a/src/pages/api/immich-proxy/asset/thumbnail/[id].ts b/src/pages/api/immich-proxy/asset/thumbnail/[id].ts index d130d41..aa29165 100644 --- a/src/pages/api/immich-proxy/asset/thumbnail/[id].ts +++ b/src/pages/api/immich-proxy/asset/thumbnail/[id].ts @@ -2,6 +2,7 @@ import { ENV } from '@/config/environment'; import { getCurrentUser } from '@/handlers/serverUtils/user.utils'; +import { getUserHeaders } from '@/helpers/user.helper'; import { NextApiRequest, NextApiResponse } from 'next' export const config = { @@ -19,6 +20,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) const targetUrl = `${ENV.IMMICH_URL}/api/assets/${id}/thumbnail?size=${size || 'thumbnail'}`; const user = await getCurrentUser(req); + if (!user) { return res.status(403).json({ message: 'Unauthorized' }) } @@ -26,10 +28,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) // Forward the request to the target API const response = await fetch(targetUrl, { method: 'GET', - headers: { + headers: getUserHeaders(user, { 'Content-Type': 'application/octet-stream', - 'Authorization': `Bearer ${user.accessToken}`, - }, + }), }) if (!response.ok) { diff --git a/src/pages/api/immich-proxy/thumbnail/[id].ts b/src/pages/api/immich-proxy/thumbnail/[id].ts index f554a1e..43f45d9 100644 --- a/src/pages/api/immich-proxy/thumbnail/[id].ts +++ b/src/pages/api/immich-proxy/thumbnail/[id].ts @@ -2,6 +2,7 @@ import { ENV } from '@/config/environment'; import { getCurrentUser } from '@/handlers/serverUtils/user.utils'; +import { getUserHeaders } from '@/helpers/user.helper'; import { NextApiRequest, NextApiResponse } from 'next' export const config = { @@ -28,10 +29,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) // Forward the request to the target API const response = await fetch(targetUrl, { method: 'GET', - headers: { + headers: getUserHeaders(currentUser, { 'Content-Type': 'application/octet-stream', - Authorization: `Bearer ${currentUser.accessToken}`, - }, + }), }) if (!response.ok) { diff --git a/src/pages/api/people/[id]/similar-faces.ts b/src/pages/api/people/[id]/similar-faces.ts index 7ba491e..e1466b7 100644 --- a/src/pages/api/people/[id]/similar-faces.ts +++ b/src/pages/api/people/[id]/similar-faces.ts @@ -26,12 +26,6 @@ export default async function handler( try { const { id, - page = 1, - perPage = 60, - nameLessOnly = false, - minimumAssetCount = 0, - sort = "assetCount", - sortOrder = "desc", } = req.query as any as IQuery; const currentUser = await getCurrentUser(req); diff --git a/src/pages/api/people/list.ts b/src/pages/api/people/list.ts index 228b564..054ba13 100644 --- a/src/pages/api/people/list.ts +++ b/src/pages/api/people/list.ts @@ -86,8 +86,6 @@ export default async function handler( } const people = await query.limit(perPage).offset((page - 1) * perPage); - - return res.status(200).json({ people, total: 10, diff --git a/src/types/user.d.ts b/src/types/user.d.ts index 45daa33..55a6ed3 100644 --- a/src/types/user.d.ts +++ b/src/types/user.d.ts @@ -15,4 +15,6 @@ export interface IUser { quotaUsageInBytes: number; status: string; license: null; + isUsingAPIKey?: boolean; + accessToken?: string; }