Skip to content

Commit 660fb1d

Browse files
authored
Merge pull request #169 from PeerPrep/bhcs/fix-401
Patch auth code
2 parents 0c78a3d + 440a3f5 commit 660fb1d

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

questions/src/middleware/auth.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import express from "express";
2-
import { NextFunction } from "express";
1+
import axios, { HttpStatusCode } from "axios";
2+
import express, { NextFunction } from "express";
33
import { Auth } from "firebase-admin/auth";
4-
import { handleCustomError, handleServerError } from "../utils";
54
import { StatusMessageType } from "../types";
6-
import axios, { HttpStatusCode } from "axios";
5+
import { handleCustomError, handleServerError } from "../utils";
76

87
const getFirebaseMiddleware = (firebaseAuth: Auth) => {
98
return async (
@@ -14,10 +13,14 @@ const getFirebaseMiddleware = (firebaseAuth: Auth) => {
1413
try {
1514
const firebaseToken = req.get("firebase-token");
1615
if (!firebaseToken) {
17-
handleCustomError(res, {
18-
type: StatusMessageType.ERROR,
19-
message: "No Firebase token provided",
20-
});
16+
handleCustomError(
17+
res,
18+
{
19+
type: StatusMessageType.ERROR,
20+
message: "No Firebase token provided",
21+
},
22+
401
23+
);
2124
return;
2225
}
2326

@@ -40,19 +43,27 @@ const getFirebaseMiddleware = (firebaseAuth: Auth) => {
4043
!usersResponse.data.payload ||
4144
!usersResponse.data.payload.role
4245
) {
43-
handleCustomError(res, {
44-
type: StatusMessageType.ERROR,
45-
message:
46-
"Error while fetching user profile. Please try again later!",
47-
});
46+
handleCustomError(
47+
res,
48+
{
49+
type: StatusMessageType.ERROR,
50+
message:
51+
"Error while fetching user profile. Please try again later!",
52+
},
53+
401
54+
);
4855
return;
4956
}
5057

5158
if (usersResponse.data.payload.role != "admin") {
52-
handleCustomError(res, {
53-
type: StatusMessageType.ERROR,
54-
message: "Only an authorized admin can perform this action!",
55-
});
59+
handleCustomError(
60+
res,
61+
{
62+
type: StatusMessageType.ERROR,
63+
message: "Only an authorized admin can perform this action!",
64+
},
65+
401
66+
);
5667
return;
5768
}
5869
}

questions/src/utils/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import {
88

99
export const handleCustomError = (
1010
res: express.Response,
11-
statusMessage: StatusMessage
11+
statusMessage: StatusMessage,
12+
statusCode?: number
1213
) => {
1314
const response: ApiResponse = {
1415
payload: EMPTY_OBJECT,
1516
statusMessage,
1617
};
1718

18-
res.status(400).json(response);
19+
res.status(statusCode ?? 400).json(response);
1920
return;
2021
};
2122

0 commit comments

Comments
 (0)