Skip to content

Commit 004225e

Browse files
committed
added query to check if user is an admin
1 parent 0a944f4 commit 004225e

File tree

6 files changed

+52
-8
lines changed

6 files changed

+52
-8
lines changed

backend/typescript/graphql/resolvers/authResolvers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ const authResolvers = {
5353
);
5454
return isAuthorized;
5555
},
56+
isAuthorizedAdmin: async (
57+
_parent: undefined,
58+
{ accessToken }: { accessToken: string },
59+
): Promise<boolean> => {
60+
const isAuthorized = await authService.isAuthorizedAdmin(
61+
accessToken,
62+
);
63+
return isAuthorized;
64+
},
5665
},
5766
Mutation: {
5867
login: async (

backend/typescript/graphql/types/authType.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const authType = gql`
2525
extend type Query {
2626
login(email: String!, password: String!): loginOK!
2727
isAuthorizedByRole(accessToken: String!, roles: [Role!]!): Boolean!
28+
isAuthorizedAdmin(accessToken: String!): Boolean!
2829
}
2930
3031
extend type Mutation {

backend/typescript/graphql/types/reviewedApplicantRecordTypes.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ const reviewedApplicantRecordTypes = gql`
77
SENIOR
88
}
99
10-
type Review {
11-
passionFSG: Int
12-
teamPlayer: Int
13-
desireToLearn: Int
14-
skill: Int
15-
skillCategory: SkillCategory
16-
}
17-
1810
input ReviewInput {
1911
passionFSG: Int
2012
teamPlayer: Int
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { DataType } from "sequelize-typescript";
2+
import { Migration } from "../umzug";
3+
4+
const TABLE_NAME = "admins";
5+
6+
export const up: Migration = async ({ context: sequelize }) => {
7+
await sequelize.getQueryInterface().addColumn(TABLE_NAME, "createdAt", {
8+
type: DataType.DATE,
9+
allowNull: false,
10+
defaultValue: sequelize.literal("NOW()"),
11+
});
12+
await sequelize.getQueryInterface().addColumn(TABLE_NAME, "updatedAt", {
13+
type: DataType.DATE,
14+
allowNull: false,
15+
defaultValue: sequelize.literal("NOW()"),
16+
});
17+
};
18+
19+
export const down: Migration = async ({ context: sequelize }) => {
20+
await sequelize.getQueryInterface().removeColumn(TABLE_NAME, "createdAt");
21+
await sequelize.getQueryInterface().removeColumn(TABLE_NAME, "updatedAt");
22+
};

backend/typescript/services/implementations/authService.ts

Lines changed: 13 additions & 0 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 Admin from "../../models/admin.model";
1011

1112
const Logger = logger(__filename);
1213

@@ -272,6 +273,18 @@ class AuthService implements IAuthService {
272273
throw error;
273274
}
274275
}
276+
277+
async isAuthorizedAdmin(accessToken: string): Promise<boolean> {
278+
try {
279+
const decodedIdToken: firebaseAdmin.auth.DecodedIdToken =
280+
await firebaseAdmin.auth().verifyIdToken(accessToken, true);
281+
const userId = await this.userService.getUserIdByAuthId(decodedIdToken.uid);
282+
const adminEntry = await Admin.findOne({ where: { userId } });
283+
return !!adminEntry;
284+
} catch (error) {
285+
return false;
286+
}
287+
}
275288
}
276289

277290
export default AuthService;

backend/typescript/services/interfaces/authService.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ interface IAuthService {
9090
* @returns true if email sent successfully
9191
*/
9292
sendSignInLink(email: string): Promise<boolean>;
93+
94+
/**
95+
* Determine if the user associated with the provided access token is an admin
96+
* @param accessToken user's access token
97+
* @returns true if Admin entry exists with userId associated with accessToken, false otherwise
98+
*/
99+
isAuthorizedAdmin(accessToken: string): Promise<boolean>;
93100
}
94101

95102
export default IAuthService;

0 commit comments

Comments
 (0)