fix: authentication fixes in proxies
Signed-off-by: Varun Raj <mailme@varunraj.in>
This commit is contained in:
@@ -36,9 +36,9 @@ export default function ProfileInfo() {
|
||||
{user && (
|
||||
<>
|
||||
<p className="text-sm">{user?.name}</p>
|
||||
<Button variant="destructive" className="mx-4" onClick={handleLogout}>
|
||||
{!user.isUsingAPIKey && <Button variant="destructive" className="mx-4" onClick={handleLogout}>
|
||||
Log Out
|
||||
</Button>
|
||||
</Button>}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -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
|
||||
})
|
||||
|
||||
@@ -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}
|
||||
}
|
||||
@@ -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,
|
||||
})
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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,
|
||||
|
||||
Vendored
+2
@@ -15,4 +15,6 @@ export interface IUser {
|
||||
quotaUsageInBytes: number;
|
||||
status: string;
|
||||
license: null;
|
||||
isUsingAPIKey?: boolean;
|
||||
accessToken?: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user