diff --git a/src/modules/Auth/AuthController.ts b/src/modules/Auth/AuthController.ts index 07c790f..32b48c0 100644 --- a/src/modules/Auth/AuthController.ts +++ b/src/modules/Auth/AuthController.ts @@ -288,3 +288,16 @@ export async function updatePassword(req: SessionRequest, res: Response) { errorHandler(res, error) } } + +export async function findForgottenAccount(req: SessionRequest, res: Response) { + try { + const { body } = await zParse(Schema.ForgottenAccount, req) + + const data = await Interactor.findForgottenAccount(body.account) + // await Interactor.updatePassword({ userid, newPassword, oldPassword }) + + res.status(200).json(data) + } catch (error) { + errorHandler(res, error) + } +} diff --git a/src/modules/Auth/AuthInteractor.ts b/src/modules/Auth/AuthInteractor.ts index 81861f7..457eab2 100644 --- a/src/modules/Auth/AuthInteractor.ts +++ b/src/modules/Auth/AuthInteractor.ts @@ -52,6 +52,22 @@ export async function authenticateUser(credentials: string, password: string) { return user } +export async function findForgottenAccount(account: string) { + const user = await Service.findByEmailOrUsername(account) + + if (!user) { + throw new HttpError('Invalid email or contact', 401) + } + + if (user.isbanned) { + throw new HttpError('Your account has been banned.', 401) + } + + const { avatar, firstname, lastname, email, username, contact_number } = user + + return { avatar, firstname, lastname, email, username, contact_number } +} + export async function registerUser(credentials: RegisterUser) { const { phone_number, email, password, confirmPassword } = credentials.body diff --git a/src/modules/Auth/AuthOpenApi.ts b/src/modules/Auth/AuthOpenApi.ts index a2fcac6..e25a04d 100644 --- a/src/modules/Auth/AuthOpenApi.ts +++ b/src/modules/Auth/AuthOpenApi.ts @@ -488,3 +488,76 @@ * type: string * description: Confirm the new password */ + +/** + * @openapi + * /api/auth/find/forgotten/account: + * post: + * summary: Find forgotten account + * tags: + * - Auth + * requestBody: + * required: true + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/ForgottenAccountJSON" + * responses: + * "200": + * description: Account found successfully + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/ForgottenAccountResponse" + * "401": + * description: Unauthorized + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/ErrorResponse" + * "400": + * description: Validation Error + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/ErrorResponse" + * "404": + * description: Not Found Error + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/ErrorResponse" + * "500": + * description: Server Error + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/ServerError" + */ + +/** + * @openapi + * components: + * schemas: + * ForgottenAccountJSON: + * type: object + * properties: + * account: + * type: string + * + * ForgottenAccountResponse: + * type: object + * properties: + * avatar: + * type: string + * firstname: + * type: string + * lastname: + * type: string + * email: + * type: string + * username: + * type: string + * contact_number: + * type: string + */ diff --git a/src/modules/Auth/AuthRouter.ts b/src/modules/Auth/AuthRouter.ts index 77c2d8f..223380a 100644 --- a/src/modules/Auth/AuthRouter.ts +++ b/src/modules/Auth/AuthRouter.ts @@ -309,3 +309,5 @@ AuthRouter.post( ) AuthRouter.post('/update/password', AuthController.updatePassword) + +AuthRouter.post('/find/forgotten/account', AuthController.findForgottenAccount) diff --git a/src/schema/AuthSchema.ts b/src/schema/AuthSchema.ts index 39f82db..c6bb4cc 100644 --- a/src/schema/AuthSchema.ts +++ b/src/schema/AuthSchema.ts @@ -371,3 +371,9 @@ export const UpdatePassword = z.object({ path: ['confirmPassword'], // path of error }), }) + +export const ForgottenAccount = z.object({ + body: z.object({ + account: z.string(), + }), +})