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
13 changes: 13 additions & 0 deletions src/http_tests/authentication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,17 @@ describe('Authentication JWT', () => {
expect(res.statusCode).toEqual(200);
expect(userBody).toHaveProperty('email', testUser.email);
});

it('should reject request with invalid token', async () => {
const invalidToken = 'this.is.an.invalid.token';

const res: Response = await request(app)
.get('/auth/me')
.set('Authorization', `Bearer ${invalidToken}`)
.expect(401);

const body = res.body as AuthResponse;
expect(body).toHaveProperty('message');
expect(typeof body.message).toBe('string');
});
});
31 changes: 31 additions & 0 deletions src/midleware/authMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { NextFunction, Request, Response } from 'express';
import jwt from 'jsonwebtoken';

const JWT_SECRET = process.env.JWT_SECRET;

if (!JWT_SECRET) {
throw new Error('JWT_SECRET is not defined');
}

// Extend the Request interface to include the user payload from the token

const authMiddleware = (req: Request, res: Response, next: NextFunction): void => {
const authHeader = req.headers.authorization;
const token = authHeader?.split(' ')[1];

if (!token) {
res.status(401).json({ message: 'No token provided' });
return;
}

jwt.verify(token, JWT_SECRET, err => {
if (err) {
res.status(401).json({ message: 'Invalid or expired token' });
return;
}

next();
});
};

export default authMiddleware;
3 changes: 2 additions & 1 deletion src/routes/authRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import jwt from 'jsonwebtoken';
import { ObjectId } from 'mongodb';

import { getDatabase } from '../database/mongo-common';
import authMiddleware from '../midleware/authMiddleware';

const router: Router = Router();
const JWT_SECRET = process.env.JWT_SECRET;
Expand Down Expand Up @@ -80,7 +81,7 @@ router.post('/login', async (req: Request, res: Response): Promise<void> => {
});

// responds with user data
router.get('/me', async (req: Request, res: Response): Promise<void> => {
router.get('/me', authMiddleware, async (req: Request, res: Response): Promise<void> => {
const token = req.headers.authorization?.split(' ')[1];

if (!token) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/git-user-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { execSync } from 'child_process';
function getGitUserName(): string {
if (process.env.GITHUB_ACTIONS === 'true') {
const githubActor = process.env.GITHUB_ACTOR;
return githubActor || 'github-actions';
return githubActor ?? 'github-actions';
} else {
try {
const name = execSync('git config --get user.name', { encoding: 'utf8' }).trim();
Expand Down