Skip to content

Commit 3751fc2

Browse files
initial commit
1 parent e010ca3 commit 3751fc2

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

backend/typescript/graphql/resolvers/authResolvers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ const authResolvers = {
5353
);
5454
return isAuthorized;
5555
},
56+
isAuthorizedReviewer: async (
57+
_parent: undefined,
58+
{accessToken, applicantRecordId}: {accessToken: string, applicantRecordId: string},
59+
): Promise<boolean> => {
60+
const isAuthorized = await authService.isAuthorizedReviewer(
61+
accessToken,
62+
applicantRecordId,
63+
);
64+
return isAuthorized;
65+
}
5666
},
5767
Mutation: {
5868
login: async (

backend/typescript/services/implementations/authService.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { AuthDTO, Role, Token } from "../../types";
77
import { getErrorMessage } from "../../utilities/errorUtils";
88
import FirebaseRestClient from "../../utilities/firebaseRestClient";
99
import logger from "../../utilities/logger";
10+
import ReviewedApplicantRecord from "../../models/reviewedApplicantRecord.model";
1011

1112
const Logger = logger(__filename);
1213

@@ -41,9 +42,8 @@ class AuthService implements IAuthService {
4142
/* eslint-disable class-methods-use-this */
4243
async generateTokenOAuth(idToken: string): Promise<AuthDTO> {
4344
try {
44-
const googleUser = await FirebaseRestClient.signInWithGoogleOAuth(
45-
idToken,
46-
);
45+
const googleUser =
46+
await FirebaseRestClient.signInWithGoogleOAuth(idToken);
4747
// googleUser.idToken refers to the Firebase Auth access token for the user
4848
const token = {
4949
accessToken: googleUser.idToken,
@@ -272,6 +272,24 @@ class AuthService implements IAuthService {
272272
throw error;
273273
}
274274
}
275+
276+
async isAuthorizedReviewer(accessToken: string, applicantRecordId: string): Promise<boolean> {
277+
try {
278+
const decodedToken: firebaseAdmin.auth.DecodedIdToken =
279+
await firebaseAdmin.auth().verifyIdToken(accessToken, true);
280+
const userId = await this.userService.getUserIdByAuthId(
281+
decodedToken.uid,
282+
);
283+
284+
const record = await ReviewedApplicantRecord.findOne({
285+
where: { applicantRecordId, userId },
286+
});
287+
288+
return !!record;
289+
} catch (error) {
290+
return false;
291+
}
292+
}
275293
}
276294

277295
export default AuthService;

backend/typescript/services/interfaces/authService.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ interface IAuthService {
9090
* @returns true if email sent successfully
9191
*/
9292
sendSignInLink(email: string): Promise<boolean>;
93+
94+
/**
95+
* Checks whether a user is an authorized reviewer for a specific applicant record.
96+
* @param accessToken user's access token
97+
* @param applicantRecordId applicant record to be accessed
98+
* @returns true if the user is authorized to access the applicant record
99+
*/
100+
isAuthorizedReviewer(accessToken: string, applicantRecordId: string): Promise<boolean>;
93101
}
94102

95103
export default IAuthService;

0 commit comments

Comments
 (0)