Skip to content

Commit f45a1ce

Browse files
authored
Merge pull request #166 from cuappdev/aayush/auth
Aayush/auth
2 parents 70597ed + 2204e0d commit f45a1ce

9 files changed

Lines changed: 205 additions & 255 deletions

File tree

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
},
2626
"homepage": "https://github.com/cuappdev/resell-backend#readme",
2727
"devDependencies": {
28-
"@types/express": "^4.17.13",
28+
"@types/body-parser": "^1.19.6",
29+
"@types/express": "^4.17.25",
2930
"@types/faker": "^5.5.5",
3031
"@types/jest": "^27.0.2",
32+
"@types/multer": "^2.0.0",
3133
"@types/node": "^16.9.0",
3234
"@types/node-fetch": "^2.6.1",
3335
"@types/swagger-ui-express": "^4.1.7",
@@ -46,12 +48,15 @@
4648
"@tensorflow-models/universal-sentence-encoder": "^1.3.3",
4749
"@tensorflow/tfjs": "^4.2.0",
4850
"@tensorflow/tfjs-node": "^4.22.0",
49-
"class-validator": "^0.14.0",
51+
"body-parser": "^2.2.2",
52+
"class-transformer": "^0.5.1",
53+
"class-validator": "^0.14.3",
5054
"cors": "^2.8.6",
5155
"dotenv": "^10.0.0",
52-
"express": "^4.17.2",
56+
"express": "^4.22.1",
5357
"faker": "^5.5.3",
5458
"firebase-admin": "^13.1.0",
59+
"multer": "^2.0.2",
5560
"node-fetch": "^2.6.1",
5661
"pg": "^8.7.1",
5762
"pgvector": "^0.2.0",

src/api/controllers/NotifController.ts

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,54 +17,64 @@ import { UserModel } from "../../models/UserModel";
1717

1818
@JsonController("notif/")
1919
export class NotifController {
20-
private notifService: NotifService;
20+
private notifService: NotifService;
2121

22-
constructor(notifService: NotifService) {
23-
this.notifService = notifService;
24-
}
22+
constructor(notifService: NotifService) {
23+
this.notifService = notifService;
24+
}
2525

26-
@Get('recent')
27-
async getRecentNotifications(@CurrentUser() user: UserModel) {
28-
return this.notifService.getRecentNotifications(user.firebaseUid);
29-
}
26+
@Get("recent")
27+
async getRecentNotifications(@CurrentUser() user: UserModel) {
28+
return this.notifService.getRecentNotifications(user.firebaseUid);
29+
}
3030

31-
@Get('new')
32-
async getUnread(@CurrentUser() user: UserModel) {
33-
return this.notifService.getUnreadNotifications(user.firebaseUid);
34-
}
31+
@Get("new")
32+
async getUnread(@CurrentUser() user: UserModel) {
33+
return this.notifService.getUnreadNotifications(user.firebaseUid);
34+
}
3535

36-
@Get('last7days')
37-
getLast7Days(@CurrentUser() user: UserModel) {
38-
return this.notifService.getNotificationsLast7Days(user.firebaseUid);
39-
}
36+
@Get("last7days")
37+
getLast7Days(@CurrentUser() user: UserModel) {
38+
return this.notifService.getNotificationsLast7Days(user.firebaseUid);
39+
}
4040

41-
@Get('last30days')
42-
getLast30Days(@CurrentUser() user: UserModel) {
43-
return this.notifService.getNotificationsLast30Days(user.firebaseUid);
44-
}
41+
@Get("last30days")
42+
getLast30Days(@CurrentUser() user: UserModel) {
43+
return this.notifService.getNotificationsLast30Days(user.firebaseUid);
44+
}
4545

46-
@Post()
47-
async sendNotif(@Body() findTokensRequest: FindTokensRequest) {
48-
return this.notifService.sendNotifs(findTokensRequest);
49-
}
46+
@Post()
47+
async sendNotif(@Body() findTokensRequest: FindTokensRequest) {
48+
return this.notifService.sendNotifs(findTokensRequest);
49+
}
5050

51-
@Post('discount')
52-
async sendDiscountNotif(@Body() discountRequest: DiscountNotificationRequest) {
53-
return this.notifService.sendDiscountNotification(discountRequest);
54-
}
51+
@Post("discount")
52+
async sendDiscountNotif(
53+
@Body() discountRequest: DiscountNotificationRequest,
54+
) {
55+
return this.notifService.sendDiscountNotification(discountRequest);
56+
}
5557

56-
@Post('request-match')
57-
async sendRequestMatchNotif(@Body() matchRequest: RequestMatchNotificationRequest) {
58-
return this.notifService.sendRequestMatchNotification(matchRequest);
59-
}
58+
@Post("request-match")
59+
async sendRequestMatchNotif(
60+
@Body() matchRequest: RequestMatchNotificationRequest,
61+
) {
62+
return this.notifService.sendRequestMatchNotification(matchRequest);
63+
}
6064

61-
@Post('markAsRead/id/:id')
62-
async markAsRead(@CurrentUser() user: UserModel, @Params() params: { id: string }) {
63-
return this.notifService.markAsRead(user.firebaseUid, params.id);
64-
}
65+
@Post("markAsRead/id/:id")
66+
async markAsRead(
67+
@CurrentUser() user: UserModel,
68+
@Params() params: { id: string },
69+
) {
70+
return this.notifService.markAsRead(user.firebaseUid, params.id);
71+
}
6572

66-
@Delete('id/:id')
67-
async deleteNotification(@CurrentUser() user: UserModel, @Params() params: { id: string }) {
68-
return this.notifService.deleteNotification(user.firebaseUid, params.id);
69-
}
73+
@Delete("id/:id")
74+
async deleteNotification(
75+
@CurrentUser() user: UserModel,
76+
@Params() params: { id: string },
77+
) {
78+
return this.notifService.deleteNotification(user.firebaseUid, params.id);
79+
}
7080
}

src/api/controllers/ReportController.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
GetReportsResponse,
2020
} from "../../types/ApiResponses";
2121
import { ReportModel } from "../../models/ReportModel";
22-
import { report } from "process";
2322
import { UuidParam } from "../validators/GenericRequests";
2423

2524
@JsonController("report/")

src/api/middlewares/ErrorHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ export class ErrorHandler implements ExpressErrorMiddlewareInterface {
1313
error: Error,
1414
request: express.Request,
1515
response: express.Response,
16-
next: (err?: any) => any,
16+
next: (err?: unknown) => unknown,
1717
): void {
1818
handleError(error, request, response, next);
1919
}
2020
}
2121

2222
function handleError(
2323
error: Error,
24-
request: express.Request,
24+
_request: express.Request,
2525
response: express.Response,
2626
next: express.NextFunction,
2727
) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {
2+
Action,
3+
ForbiddenError,
4+
NotFoundError,
5+
UnauthorizedError,
6+
} from "routing-controllers";
7+
import { firebaseAdmin } from "../../firebase";
8+
import { getManager } from "typeorm";
9+
import { UserModel } from "../../models/UserModel";
10+
11+
export const FirebaseCurrentUserChecker = async (
12+
action: Action,
13+
): Promise<UserModel> => {
14+
const authHeader = action.request.headers["authorization"];
15+
if (!authHeader) {
16+
throw new UnauthorizedError("No authorization token provided");
17+
}
18+
const token = authHeader.split(" ")[1];
19+
if (!token) {
20+
throw new UnauthorizedError("Invalid authorization token format");
21+
}
22+
23+
try {
24+
// Verify the token using Firebase Admin SDK
25+
const decodedToken = await firebaseAdmin.auth().verifyIdToken(token);
26+
const userId = decodedToken.uid;
27+
const email = decodedToken.email;
28+
// Enforce Cornell email domain restriction
29+
if (email && !email.endsWith("@cornell.edu")) {
30+
throw new ForbiddenError("Only Cornell email addresses are allowed");
31+
}
32+
// Fetch and return the user from the database
33+
const user = await getManager().findOne(UserModel, {
34+
firebaseUid: userId,
35+
});
36+
if (!user) {
37+
throw new NotFoundError("User not found");
38+
}
39+
return user;
40+
} catch (error) {
41+
throw new UnauthorizedError("Invalid or expired authorization token");
42+
}
43+
};

src/api/validators/GenericRequests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IsEmail, IsNumber, IsString, IsUUID } from "class-validator";
1+
import { IsEmail, IsString, IsUUID } from "class-validator";
22

33
import { Uuid } from "../../types";
44

0 commit comments

Comments
 (0)