Skip to content

Commit

Permalink
Merge pull request #41 from uwblueprint/F24/sayi/update-createuser
Browse files Browse the repository at this point in the history
Update createUser function
  • Loading branch information
sthuray authored Oct 29, 2024
2 parents 9a43dbb + cdf3fb5 commit fe50390
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 63 deletions.
3 changes: 0 additions & 3 deletions backend/typescript/middlewares/validators/userValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ export const createUserDtoValidator = async (
if (!validatePrimitive(req.body.role, "string")) {
return res.status(400).send(getApiValidationError("role", "string"));
}
if (!validatePrimitive(req.body.password, "string")) {
return res.status(400).send(getApiValidationError("password", "string"));
}
if (
req.body.skillLevel !== undefined &&
req.body.skillLevel !== null &&
Expand Down
4 changes: 1 addition & 3 deletions backend/typescript/rest/authRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import IAuthService from "../services/interfaces/authService";
import IEmailService from "../services/interfaces/emailService";
import IUserService from "../services/interfaces/userService";
import { getErrorMessage } from "../utilities/errorUtils";
import { Role, UserStatus } from "../types";
import { Role } from "../types";

const authRouter: Router = Router();
const userService: IUserService = new UserService();
Expand Down Expand Up @@ -53,12 +53,10 @@ authRouter.post("/register", registerRequestValidator, async (req, res) => {
lastName: req.body.lastName,
email: req.body.email,
role: req.body.role ?? Role.VOLUNTEER,
status: req.body.status ?? UserStatus.ACTIVE, // TODO: make this default to inactive once user registration flow is done
skillLevel: req.body.skillLevel ?? null,
canSeeAllLogs: req.body.canSeeAllLogs ?? null,
canAssignUsersToTasks: req.body.canAssignUsersToTasks ?? null,
phoneNumber: req.body.phoneNumber ?? null,
password: req.body.password,
});

const authDTO = await authService.generateToken(
Expand Down
9 changes: 4 additions & 5 deletions backend/typescript/rest/userRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import UserService from "../services/implementations/userService";
import IAuthService from "../services/interfaces/authService";
import IEmailService from "../services/interfaces/emailService";
import IUserService from "../services/interfaces/userService";
import { Role, UserDTO, UserStatus } from "../types";
import { Role, UserDTO } from "../types";
import {
getErrorMessage,
NotFoundError,
Expand Down Expand Up @@ -99,23 +99,22 @@ userRouter.get("/", async (req, res) => {
}
});

// This endpoint is for testing purposes
/* Create a user */
userRouter.post("/", createUserDtoValidator, async (req, res) => {
try {
const newUser = await userService.createUser({
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
role: req.body.role ?? Role.VOLUNTEER,
status: UserStatus.INVITED,
role: req.body.role,
skillLevel: req.body.skillLevel ?? null,
canSeeAllLogs: req.body.canSeeAllLogs ?? null,
canAssignUsersToTasks: req.body.canSeeAllUsers ?? null,
phoneNumber: req.body.phoneNumber ?? null,
password: req.body.password,
});

await authService.sendEmailVerificationLink(req.body.email);
// await authService.sendEmailVerificationLink(req.body.email);

res.status(201).json(newUser);
} catch (error: unknown) {
Expand Down
29 changes: 11 additions & 18 deletions backend/typescript/services/implementations/authService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as firebaseAdmin from "firebase-admin";
import IAuthService from "../interfaces/authService";
import IEmailService from "../interfaces/emailService";
import IUserService from "../interfaces/userService";
import { AuthDTO, Role, Token, UserStatus } from "../../types";
import { AuthDTO, Role, Token } from "../../types";
import { getErrorMessage } from "../../utilities/errorUtils";
import FirebaseRestClient from "../../utilities/firebaseRestClient";
import logger from "../../utilities/logger";
Expand Down Expand Up @@ -58,18 +58,12 @@ class AuthService implements IAuthService {
/* eslint-disable-next-line no-empty */
} catch (error) {}

const user = await this.userService.createUser(
{
firstName: googleUser.firstName,
lastName: googleUser.lastName,
email: googleUser.email,
role: Role.STAFF,
status: UserStatus.ACTIVE,
password: "",
},
googleUser.localId,
"GOOGLE",
);
const user = await this.userService.createUser({
firstName: googleUser.firstName,
lastName: googleUser.lastName,
email: googleUser.email,
role: Role.STAFF,
});

return { ...token, ...user };
} catch (error) {
Expand Down Expand Up @@ -175,12 +169,11 @@ class AuthService implements IAuthService {
const userRole = await this.userService.getUserRoleByAuthId(
decodedIdToken.uid,
);
// const firebaseUser = await firebaseAdmin
// .auth()
// .getUser(decodedIdToken.uid);

const firebaseUser = await firebaseAdmin
.auth()
.getUser(decodedIdToken.uid);

return firebaseUser.emailVerified && roles.has(userRole);
return /* firebaseUser.emailVerified && */ roles.has(userRole);
} catch (error) {
return false;
}
Expand Down
29 changes: 12 additions & 17 deletions backend/typescript/services/implementations/userService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as firebaseAdmin from "firebase-admin";
import IUserService from "../interfaces/userService";
import { CreateUserDTO, Role, UpdateUserDTO, UserDTO } from "../../types";
import {
CreateUserDTO,
Role,
UpdateUserDTO,
UserDTO,
UserStatus,
} from "../../types";
import { getErrorMessage, NotFoundError } from "../../utilities/errorUtils";
import logger from "../../utilities/logger";
import PgUser from "../../models/user.model";
Expand Down Expand Up @@ -165,33 +171,22 @@ class UserService implements IUserService {
return userDtos;
}

async createUser(
user: CreateUserDTO,
authId?: string,
signUpMethod = "PASSWORD",
): Promise<UserDTO> {
async createUser(user: CreateUserDTO): Promise<UserDTO> {
let newUser: PgUser;
let firebaseUser: firebaseAdmin.auth.UserRecord;

try {
if (signUpMethod === "GOOGLE") {
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */
firebaseUser = await firebaseAdmin.auth().getUser(authId!);
} else {
// signUpMethod === PASSWORD
firebaseUser = await firebaseAdmin.auth().createUser({
email: user.email,
password: user.password,
});
}
firebaseUser = await firebaseAdmin.auth().createUser({
email: user.email,
});

try {
newUser = await PgUser.create({
first_name: user.firstName,
last_name: user.lastName,
auth_id: firebaseUser.uid,
role: user.role,
status: user.status,
status: UserStatus.INVITED,
email: firebaseUser.email ?? "",
skill_level: user.skillLevel,
can_see_all_logs: user.canSeeAllLogs,
Expand Down
16 changes: 2 additions & 14 deletions backend/typescript/services/interfaces/userService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
CreateUserDTO,
Role,
SignUpMethod,
UpdateUserDTO,
UserDTO,
} from "../../types";
import { CreateUserDTO, Role, UpdateUserDTO, UserDTO } from "../../types";

interface IUserService {
/**
Expand Down Expand Up @@ -57,16 +51,10 @@ interface IUserService {
/**
* Create a user, email verification configurable
* @param user the user to be created
* @param authId the user's firebase auth id, optional
* @param signUpMethod the method user used to signup
* @returns a UserDTO with the created user's information
* @throws Error if user creation fails
*/
createUser(
user: CreateUserDTO,
authId?: string,
signUpMethod?: SignUpMethod,
): Promise<UserDTO>;
createUser(user: CreateUserDTO): Promise<UserDTO>;

/**
* Update a user.
Expand Down
4 changes: 1 addition & 3 deletions backend/typescript/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type UserDTO = {
phoneNumber?: string | null;
};

export type CreateUserDTO = Omit<UserDTO, "id"> & { password: string };
export type CreateUserDTO = Omit<UserDTO, "id" | "status">;

export type UpdateUserDTO = Omit<UserDTO, "id">;

Expand Down Expand Up @@ -66,5 +66,3 @@ export type NodemailerConfig = {
refreshToken: string;
};
};

export type SignUpMethod = "PASSWORD" | "GOOGLE";

0 comments on commit fe50390

Please sign in to comment.