-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/lisasiliu/add-programs-form-backend-…
…route
- Loading branch information
Showing
18 changed files
with
312 additions
and
19 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# Firebase Service Account Key | ||
src/firebase/ServiceAccountKey.json | ||
|
||
.env | ||
node_modules/ | ||
.eslintcache | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
import { validationResult } from "express-validator"; | ||
import admin from "firebase-admin"; | ||
|
||
import UserModel from "../models/user"; | ||
import { firebaseAuth } from "../util/firebase"; | ||
import validationErrorParser from "../util/validationErrorParser"; | ||
|
||
// Define the type for req.body | ||
type CreateUserRequestBody = { | ||
name: string; | ||
accountType: "admin" | "team"; | ||
email: string; | ||
password: string; | ||
}; | ||
|
||
export const createUser = async ( | ||
req: Request<Record<string, never>, Record<string, never>, CreateUserRequestBody>, | ||
res: Response, | ||
nxt: NextFunction, | ||
) => { | ||
try { | ||
// Check for validation errors | ||
const errors = validationResult(req); | ||
|
||
validationErrorParser(errors); | ||
|
||
const { name, accountType, email, password } = req.body; | ||
|
||
// Create user in Firebase | ||
const userRecord = await firebaseAuth.createUser({ | ||
email, | ||
password, | ||
} as admin.auth.CreateRequest); // Type assertion | ||
|
||
// Set custom claim for accountType (“admin” | “team”) | ||
await firebaseAuth.setCustomUserClaims(userRecord.uid, { accountType }); | ||
|
||
const newUser = await UserModel.create({ | ||
Check warning on line 39 in backend/src/controllers/user.ts GitHub Actions / Backend lint and style check
Check warning on line 39 in backend/src/controllers/user.ts GitHub Actions / Backend lint and style check
|
||
_id: userRecord.uid, // Set document id to firebaseUID (Linkage between Firebase and MongoDB) | ||
name, | ||
accountType, | ||
// approvalStatus default false in User constructor | ||
}); | ||
|
||
res.status(201).json(newUser); | ||
} catch (error) { | ||
console.error(error); | ||
nxt(error); | ||
} | ||
|
||
return; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./errors"; | ||
export * from "./internal"; | ||
export * from "./validation"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import { CustomError } from "../errors"; | ||
import { CustomError } from "./errors"; | ||
|
||
const USER_CREATION_UNSUCCESSFUL = "User not created successfully"; | ||
|
||
export class ValidationError extends CustomError { | ||
constructor(message: string) { | ||
super(0, 400, "VALIDATION ERROR: " + message); | ||
constructor(code: number, status: number, message: string) { | ||
super(code, status, "VALIDATION ERROR: " + message); | ||
} | ||
static USER_CREATION_UNSUCCESSFUL = new ValidationError(1, 400, USER_CREATION_UNSUCCESSFUL); | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import mongoose, { InferSchemaType } from "mongoose"; | ||
|
||
// export type UserDocument = { | ||
// name: string; | ||
// accountType: "admin" | "team"; // NOTE Also stored on Firebase using Custom Claims | ||
// approvalStatus: boolean; | ||
// }; | ||
|
||
const userSchema = new mongoose.Schema({ | ||
_id: { type: String, required: true }, // Set _id to firebaseUid; Linkage between firebase account and user document on MongoDb | ||
name: { type: String, required: true }, | ||
accountType: { type: String, enum: ["admin", "team"], required: true }, | ||
approvalStatus: { type: Boolean, default: false }, // default false | ||
}); | ||
|
||
type User = InferSchemaType<typeof userSchema>; | ||
|
||
export default mongoose.model<User>("User", userSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import express from "express"; | ||
|
||
import * as UserController from "../controllers/user"; | ||
import * as UserValidator from "../validators/user"; | ||
|
||
const router = express.Router(); | ||
|
||
router.use(express.json()); | ||
|
||
router.post("/", UserValidator.createUser, UserController.createUser); | ||
|
||
export { router as userRouter }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ValidationChain, body } from "express-validator"; | ||
|
||
export const createUser: ValidationChain[] = [ | ||
body("name").notEmpty().isString(), | ||
body("accountType").notEmpty().isIn(["admin", "team"]), | ||
body("email").notEmpty().isEmail(), | ||
body("password").notEmpty().isString().isLength({ min: 6 }), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.