Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,4 @@ stephen-ghcr-pat
# End of https://www.toptal.com/developers/gitignore/api/node,firebase
backend/serviceAccountKey.json
frontend/node_modules/*
backend/node_modules/*
2 changes: 1 addition & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM public.ecr.aws/lambda/nodejs:20

COPY package*.json ./
COPY package.json package-lock.json ./
RUN npm ci --omit=dev

COPY . .
Expand Down
3 changes: 3 additions & 0 deletions backend/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ describe('GET /ingredients/:upc/:userId', () => {
const userData = { allergens: ['peanut'], intolerances: ['gluten'] };
const mockRef = {
get: jest.fn().mockResolvedValue({ exists: true, data: () => userData }),
collection: jest.fn().mockReturnValue({ add: jest.fn().mockResolvedValue({}) }),
};
const mockCollection = { doc: jest.fn().mockReturnValue(mockRef) };
getDb.mockResolvedValue({ collection: jest.fn().mockReturnValue(mockCollection) });
Expand All @@ -120,6 +121,7 @@ describe('GET /ingredients/:upc/:userId', () => {
const userData = { allergens: ['peanut'], intolerances: ['gluten'] };
const mockRef = {
get: jest.fn().mockResolvedValue({ exists: true, data: () => userData }),
collection: jest.fn().mockReturnValue({ add: jest.fn().mockResolvedValue({}) }),
};
const mockCollection = { doc: jest.fn().mockReturnValue(mockRef) };
getDb.mockResolvedValue({ collection: jest.fn().mockReturnValue(mockCollection) });
Expand All @@ -143,6 +145,7 @@ describe('GET /ingredients/:upc/:userId', () => {
const userData = { allergens: ['peanut'], intolerances: ['wheat'] };
const mockRef = {
get: jest.fn().mockResolvedValue({ exists: true, data: () => userData }),
collection: jest.fn().mockReturnValue({ add: jest.fn().mockResolvedValue({}) }),
};
const mockCollection = { doc: jest.fn().mockReturnValue(mockRef) };
getDb.mockResolvedValue({ collection: jest.fn().mockReturnValue(mockCollection) });
Expand Down
6 changes: 5 additions & 1 deletion backend/middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ const getDb = require('../firebase');

async function requireAuth(req, res, next) {
if (req.method === 'OPTIONS') {
console.log(`[auth] OPTIONS ${req.path} - skipping auth`);
return next();
}

const authHeader = req.headers.authorization;

console.log(`[auth] ${req.method} ${req.path} - auth header present: ${Boolean(authHeader)}`);

if (!authHeader || !authHeader.startsWith('Bearer ')) {
return res.status(401).json({ error: 'Missing or invalid Authorization header' });
}

const token = authHeader.split('Bearer ')[1];
const token = authHeader.slice(7).trim();

try {
await getDb();
req.user = await admin.auth().verifyIdToken(token);
console.log(`[auth] ${req.method} ${req.path} - authenticated user: ${req.user.uid}`);
next();
} catch (err) {
console.error(`[auth] ${req.method} ${req.path} - token verification failed:`, err.message);
Expand Down
Loading
Loading