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
23 changes: 19 additions & 4 deletions server/src/middleware/authMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import type { NextFunction, Request, Response } from "express";
import jwt from "jsonwebtoken";

import { getJwtSecret } from "../config/jwt";
import { prisma } from "../lib/prisma";
import { HttpError } from "../utils/httpError";
import { toPublicPlan } from "../utils/plan";

type AccessTokenPayload = {
id: string;
Expand All @@ -29,7 +31,7 @@ const isAccessTokenPayload = (payload: unknown): payload is AccessTokenPayload =
typeof payload.email === "string" &&
typeof payload.plan === "string";

export const authMiddleware = (
export const authMiddleware = async (
request: Request,
_response: Response,
next: NextFunction,
Expand All @@ -41,10 +43,23 @@ export const authMiddleware = (
throw new HttpError(401, "Authorization token is invalid.");
}

const user = await prisma.user.findUnique({
where: { id: payload.id },
select: {
id: true,
email: true,
plan: true,
},
});

if (!user) {
throw new HttpError(401, "Session is no longer valid. Please sign in again.");
}

request.user = {
id: payload.id,
email: payload.email,
plan: payload.plan,
id: user.id,
email: user.email,
plan: toPublicPlan(user.plan),
};

next();
Expand Down
6 changes: 3 additions & 3 deletions server/src/routes/proposalRouter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Router } from "express";
import { Router, type RequestHandler } from "express";

import { authMiddleware } from "../middleware/authMiddleware";
import {
Expand All @@ -15,13 +15,13 @@ import {

const router = Router();

const optionalAuth: typeof authMiddleware = (req, res, next) => {
const optionalAuth: RequestHandler = (req, res, next) => {
if (!req.header("authorization")) {
next();
return;
}

authMiddleware(req, res, next);
void authMiddleware(req, res, next);
};

router.get("/:id/view", viewPublicProposal);
Expand Down
21 changes: 21 additions & 0 deletions server/test/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect, beforeAll } from 'vitest';
import request from 'supertest';
import jwt from 'jsonwebtoken';
import { testApp, testUser, prisma } from './setup';

describe('Auth Endpoints', () => {
Expand Down Expand Up @@ -197,5 +198,25 @@ describe('Auth Endpoints', () => {

expect(response.body).toHaveProperty('plan');
});

it('should reject a validly signed token when the user no longer exists', async () => {
const staleAccessToken = jwt.sign(
{
id: 'deleted-user-id',
email: 'deleted@example.com',
plan: 'free',
},
process.env.JWT_SECRET || 'test-secret',
{ expiresIn: '15m' },
);

const response = await request(testApp)
.get('/payment/status')
.set('Authorization', `Bearer ${staleAccessToken}`)
.expect(401);

expect(response.body).toHaveProperty('message');
expect(response.body.message).toContain('sign in again');
});
});
});
Loading