Skip to content

Commit

Permalink
patient authenticcation validation moved to use case
Browse files Browse the repository at this point in the history
  • Loading branch information
sinanptm committed Sep 15, 2024
1 parent 991c04a commit e3b909c
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions server/src/use_case/patient/AuthenticationUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ export default class AuthenticationUseCase {
) { }

async register(patient: IPatient) {
this.validatorService.validateRequiredFields({ email: patient.email, password: patient.password, name: patient.name, phone: patient.phone })
this.validatorService.validateEmailFormat(patient.email!);
this.validatorService.validatePassword(patient.password!);
if (!patient.name?.trim()) throw new Error("Name is required");
if (!patient.phone?.toString().trim()) throw new Error("Phone number is required");
this.validatorService.validateLength(patient.name!, 3, 20);
this.validatorService.validatePhoneNumber(patient.phone!);

patient.password = await this.passwordService.hash(patient.password!);
const { _id } = await this.patientRepository.create(patient);
Expand All @@ -35,13 +36,12 @@ export default class AuthenticationUseCase {

async login(patient: IPatient): Promise<{ email: string } | null> {
this.validatorService.validateEmailFormat(patient.email!);
if (!patient.password?.trim()) throw new Error("Password is required");
this.validatorService.validatePassword(patient.password!);

const foundedPatient = await this.patientRepository.findByEmailWithCredentials(patient.email!);
if (!foundedPatient) throw new Error("Invalid Credentials");

if (!foundedPatient.password) throw new Error("Patient has no Password");

if (foundedPatient.isBlocked) throw new Error("Patient is Blocked");

const isPasswordValid = await this.passwordService.compare(patient.password!, foundedPatient.password!);
Expand All @@ -66,7 +66,7 @@ export default class AuthenticationUseCase {

async oAuthSignin(email: string, name: string, profile?: string): Promise<TokensResponse> {
this.validatorService.validateEmailFormat(email);
if (!name) throw new Error("Name is required");
this.validatorService.validateLength(name, 3, 20);

let patient = await this.patientRepository.findByEmail(email);
if (!patient) {
Expand Down

1 comment on commit e3b909c

@vercel
Copy link

@vercel vercel bot commented on e3b909c Sep 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.