Skip to content

Commit

Permalink
create separate type for user data with password. password should not…
Browse files Browse the repository at this point in the history
… leave server once stored
  • Loading branch information
rafa-lopes-pt committed Jun 22, 2024
1 parent 1bb3bfa commit e6d083d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
16 changes: 8 additions & 8 deletions backend/src/repositories/Auth.repository.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import { UserSchemaType } from "../../../shared/schemas/user.schema";

import Repository from "./Repository";
import { DbUserSchemaType } from "./auth/DbUser.type";

const USERS_DATABASE = "users";
const USERS_COLLECTION = "users_list";

export default class AuthRepository extends Repository<UserSchemaType> {
insert(data: UserSchemaType & { password: string }) {
export default class AuthRepository extends Repository<DbUserSchemaType> {
insert(data: DbUserSchemaType & { password: string }) {
return this.client.insertOne(USERS_DATABASE, USERS_COLLECTION, data);
}

find(filters: Partial<UserSchemaType>) {
find(filters: Partial<DbUserSchemaType>) {
return this.client.find(USERS_DATABASE, USERS_COLLECTION, filters);
}

findOne(filters: Partial<UserSchemaType>) {
findOne(filters: Partial<DbUserSchemaType>) {
return this.client.findOne(USERS_DATABASE, USERS_COLLECTION, filters);
}

has(filters: Partial<UserSchemaType>) {
has(filters: Partial<DbUserSchemaType>) {
return this.client.has(USERS_DATABASE, USERS_COLLECTION, filters);
}
update(filters: Partial<UserSchemaType>, data: UserSchemaType) {
update(filters: Partial<DbUserSchemaType>, data: DbUserSchemaType) {
return this.client.updateOne(
USERS_DATABASE,
USERS_COLLECTION,
filters,
data
);
}
delete(filters: Partial<UserSchemaType>) {
delete(filters: Partial<DbUserSchemaType>) {
return this.client.deleteOne(USERS_DATABASE, USERS_COLLECTION, filters);
}
}
3 changes: 3 additions & 0 deletions backend/src/repositories/auth/DbUser.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { UserSchemaType } from "../../../../shared/schemas/user.schema";

export type DbUserSchemaType = UserSchemaType & { password: string };
8 changes: 4 additions & 4 deletions backend/src/routes/controllers/auth/login.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { NextFunction, Request, Response } from "express";
import { WithId } from "mongodb";
import HTTPCodes from "simple-http-codes";
import { authRepo } from "./auth.router";
import { DbUserSchemaType } from "../../../repositories/auth/DbUser.type";
import HttpError from "../../../utils/HttpError";
import { compareHash, signToken } from "../../../utils/crypto";
import { UserSchemaType } from "../../../../../shared/schemas/user.schema";
import { WithId } from "mongodb";
import { authRepo } from "./auth.router";

export default async function loginController(
req: Request,
Expand All @@ -15,7 +15,7 @@ export default async function loginController(

//validate email and retrieve used data

let existingUser: WithId<UserSchemaType> | undefined;
let existingUser: WithId<DbUserSchemaType> | undefined;

try {
const response = await authRepo.findOne({ email });
Expand Down
3 changes: 2 additions & 1 deletion backend/src/routes/controllers/auth/signup.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { UserSchemaType } from "../../../../../shared/schemas/user.schema";
import HttpError from "../../../utils/HttpError";
import { hashData } from "../../../utils/crypto";
import { authRepo } from "./auth.router";
import { DbUserSchemaType } from "../../../repositories/auth/DbUser.type";

export default async function signupController(
req: Request,
Expand Down Expand Up @@ -35,7 +36,7 @@ export default async function signupController(

const hashedPassword = await hashData(password);

const user: UserSchemaType = {
const user: DbUserSchemaType = {
name,
email,
password: hashedPassword,
Expand Down

0 comments on commit e6d083d

Please sign in to comment.