Skip to content

Commit 003e790

Browse files
authored
Merge pull request #2042 from Northeastern-Electric-Racing/#1502-remove-any-type
#1502: removed any types in auth.utils.ts
2 parents af49dce + 1b169c6 commit 003e790

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

src/backend/src/utils/auth.utils.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import jwt from 'jsonwebtoken';
2-
import { Request, Response } from 'express';
2+
import { Request, Response, NextFunction } from 'express';
3+
import { JwtPayload, VerifyErrors } from 'jsonwebtoken';
34
import prisma from '../prisma/prisma';
45
import { NotFoundException } from './errors.utils';
56
import { User, User_Secure_Settings, User_Settings } from '@prisma/client';
@@ -24,7 +25,7 @@ export const prodHeaders = [
2425
];
2526

2627
// middleware function for production that will enforce jwt authorization
27-
export const requireJwtProd = (req: Request, res: Response, next: any) => {
28+
export const requireJwtProd = (req: Request, res: Response, next: NextFunction) => {
2829
if (
2930
req.path === '/users/auth/login' || // logins dont have cookies yet
3031
req.path === '/' || // base route is available so aws can listen and check the health
@@ -36,9 +37,12 @@ export const requireJwtProd = (req: Request, res: Response, next: any) => {
3637

3738
if (!token) return res.status(401).json({ message: 'Authentication Failed: Cookie not found!' });
3839

39-
jwt.verify(token, TOKEN_SECRET, (err: any, decoded: any) => {
40+
jwt.verify(token, TOKEN_SECRET, (err: VerifyErrors | null, decoded: string | JwtPayload | undefined) => {
4041
if (err) return res.status(401).json({ message: 'Authentication Failed: Invalid JWT!' });
4142

43+
if (!decoded || typeof decoded === 'string') {
44+
return res.status(401).json({ message: 'Authentication Failed: Invalid JWT payload!' });
45+
}
4246
res.locals.userId = parseInt(decoded.userId);
4347

4448
next();
@@ -47,7 +51,7 @@ export const requireJwtProd = (req: Request, res: Response, next: any) => {
4751
};
4852

4953
// middleware function for development that will enforce jwt authorization
50-
export const requireJwtDev = (req: Request, res: Response, next: any) => {
54+
export const requireJwtDev = (req: Request, res: Response, next: NextFunction) => {
5155
if (
5256
req.path === '/users/auth/login/dev' || // logins dont have cookies yet
5357
req.path === '/' || // base route is available so aws can listen and check the health

0 commit comments

Comments
 (0)