Skip to content

Commit

Permalink
create function and route to modify user claims
Browse files Browse the repository at this point in the history
  • Loading branch information
jiholee17 committed Oct 30, 2023
1 parent 82e19da commit 79687a1
Show file tree
Hide file tree
Showing 7 changed files with 1,548 additions and 5 deletions.
6 changes: 2 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"editor.tabSize": 2,
"editor.rulers": [
80
],
"editor.rulers": [80],
"editor.formatOnSave": true
}
}
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
serviceAccountKey.json
2 changes: 2 additions & 0 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import DocRouter from "./customers/views";
import swaggerUI from "swagger-ui-express";
import spec from "../api-spec.json";
import { dbConnect } from "./database";
import userRouter from "./users/views";

const app = express();

Expand All @@ -15,6 +16,7 @@ app.use("/api-docs", swaggerUI.serve, swaggerUI.setup(spec));
* Sub-routers for our main router, we should have one sub-router per "entity" in the application
*/
app.use("/clusters", DocRouter);
app.use("/users", userRouter);

/**
* Some dummy routes to illustrate express syntax
Expand Down
28 changes: 27 additions & 1 deletion backend/src/users/controllers.ts
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
// Controllers here, following the pattern in ../customers/controllers.ts
import * as admin from 'firebase-admin'

const serviceAccount = require('../../serviceAccountKey.json')

admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
})

/**
* Modify the claim of a user given user ID and new claim
*/
export function changeClaim(uid: string, new_claim: number) {
const claims = {
mobile_entry: new_claim == 0,
web_entry: new_claim == 1,
evaluator: new_claim == 2,
admin: new_claim == 3
};
admin.auth().setCustomUserClaims(uid, claims)
.catch(
() => {
console.log("ERROR: User with uid " + uid + " not found.");
}
)
}

export default { changeClaim };
14 changes: 14 additions & 0 deletions backend/src/users/views.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// use Controllers here, just as we did in ../users/controllers.ts

import { Router } from "express";
import { successJson } from "../utils/jsonResponses";
import UserController from "./controllers";

const userRouter = Router();

Expand All @@ -12,4 +14,16 @@ userRouter.post("/", (req, res) => {
res.send(req.body);
});

/** Modify an existing user's claims to be new_claim */
userRouter.post("/modify/", (req, res) => {
const { uid, new_claim } = req.body;
res
.status(200)
.send(
successJson(
UserController.changeClaim(uid, new_claim)
)
);
});

export default userRouter;
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"firebase-admin": "^11.11.0"
}
}
Loading

0 comments on commit 79687a1

Please sign in to comment.