import { Injectable } from '@angular/core' import { HttpClient, HttpParams } from '@angular/common/http' import { environment } from 'src/environments/environment' @Injectable({ providedIn: 'root', }) export class PasswordService { url = '' constructor(private httpClient: HttpClient) { this.url = environment.environmentUrl } /** * Reset the user password, a new temporary password will be provided via email * * @param email User email * @returns If success returns 'Password reseted successfully' */ resetPassword(email: string): Promise { return this.httpClient .post( this.url + '/auth/password/reset', { data: { user: email, }, }, { responseType: 'text' }, ) .toPromise() } /** * Reset the user password, a new temporary password will be provided via email * * @param email User email * @param newPassword User new password * @param code Code provided via email after reset flow has started * @returns If success returns 'Password changed successfully' */ confirmResetPassword( email: string, newPassword: string, code: string, ): Promise { return this.httpClient .post( this.url + '/auth/password/reset/confirm', { data: { user: email, code, newPassword, }, }, { responseType: 'text' }, ) .toPromise() } /** * Change Password for first time * * @param email Email registerd in Cognito. * @param oldPassword First password sent through email from Cognito. * @param newPassword New password entered by the user. * @returns If success, returns 'Password updated successfully' */ confirmFirstPassword( email: string, oldPassword: string, newPassword: string, ): Promise { return this.httpClient .post( this.url + '/auth/password/change', { data: { user: email, oldPassword, password: newPassword, }, }, { responseType: 'text', params: new HttpParams().set('first', 'true'), }, ) .toPromise() } }