Forgot password flow
This commit is contained in:
@@ -37,3 +37,25 @@ export interface RegisterResponse {
|
||||
message: string;
|
||||
data: AuthUser;
|
||||
}
|
||||
|
||||
export interface ResetPasswordAttemptResponse {
|
||||
message: string;
|
||||
status: 'pending';
|
||||
}
|
||||
|
||||
export interface ValidateResetPasswordAttemptResponse {
|
||||
message: string;
|
||||
status: 'validated';
|
||||
}
|
||||
|
||||
export interface ResetPasswordPayload {
|
||||
email: string;
|
||||
codigo: string;
|
||||
password: string;
|
||||
password_confirmation: string;
|
||||
}
|
||||
|
||||
export interface ResetPasswordResponse {
|
||||
message: string;
|
||||
status: 'used';
|
||||
}
|
||||
|
||||
@@ -174,6 +174,123 @@ describe('AuthService', () => {
|
||||
httpController.verify();
|
||||
});
|
||||
|
||||
it('requests a password reset and exposes the final HTTP status', () => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
provideHttpClient(),
|
||||
provideHttpClientTesting(),
|
||||
AuthService,
|
||||
{ provide: CookieService, useValue: createCookieServiceStub() },
|
||||
{
|
||||
provide: TenantService,
|
||||
useValue: { getTenant: () => ({ codigo: 'tenant-test' }) }
|
||||
},
|
||||
TransferState
|
||||
]
|
||||
});
|
||||
|
||||
const service = TestBed.inject(AuthService);
|
||||
const httpController = TestBed.inject(HttpTestingController);
|
||||
|
||||
service.requestPasswordReset('ada@example.com').subscribe((response) => {
|
||||
expect(response.status).toBe(202);
|
||||
expect(response.body?.status).toBe('pending');
|
||||
});
|
||||
|
||||
const request = httpController.expectOne(`${environment.url}password/reset-attempts`);
|
||||
expect(request.request.method).toBe('POST');
|
||||
expect(request.request.body).toEqual({
|
||||
email: 'ada@example.com',
|
||||
tenant_codigo: 'tenant-test'
|
||||
});
|
||||
request.flush(
|
||||
{
|
||||
message: 'Si el email está registrado, recibirás un código.',
|
||||
status: 'pending'
|
||||
},
|
||||
{ status: 202, statusText: 'Accepted' }
|
||||
);
|
||||
|
||||
httpController.verify();
|
||||
});
|
||||
|
||||
it('validates a password reset code and exposes its final status', () => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
provideHttpClient(),
|
||||
provideHttpClientTesting(),
|
||||
AuthService,
|
||||
{ provide: CookieService, useValue: createCookieServiceStub() },
|
||||
TransferState
|
||||
]
|
||||
});
|
||||
|
||||
const service = TestBed.inject(AuthService);
|
||||
const httpController = TestBed.inject(HttpTestingController);
|
||||
|
||||
service.validatePasswordResetCode('ada@example.com', '0123').subscribe((response) => {
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body?.status).toBe('validated');
|
||||
});
|
||||
|
||||
const request = httpController.expectOne(
|
||||
`${environment.url}password/reset-attempts/validate`
|
||||
);
|
||||
expect(request.request.method).toBe('POST');
|
||||
expect(request.request.body).toEqual({
|
||||
email: 'ada@example.com',
|
||||
codigo: '0123'
|
||||
});
|
||||
request.flush(
|
||||
{
|
||||
message: 'Código validado correctamente.',
|
||||
status: 'validated'
|
||||
},
|
||||
{ status: 200, statusText: 'OK' }
|
||||
);
|
||||
|
||||
httpController.verify();
|
||||
});
|
||||
|
||||
it('resets the password and exposes the consumed attempt status', () => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
provideHttpClient(),
|
||||
provideHttpClientTesting(),
|
||||
AuthService,
|
||||
{ provide: CookieService, useValue: createCookieServiceStub() },
|
||||
TransferState
|
||||
]
|
||||
});
|
||||
|
||||
const service = TestBed.inject(AuthService);
|
||||
const httpController = TestBed.inject(HttpTestingController);
|
||||
const payload = {
|
||||
email: 'ada@example.com',
|
||||
codigo: '0123',
|
||||
password: 'NewSecret!456',
|
||||
password_confirmation: 'NewSecret!456'
|
||||
};
|
||||
|
||||
service.resetPassword(payload).subscribe((response) => {
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body?.status).toBe('used');
|
||||
});
|
||||
|
||||
const request = httpController.expectOne(`${environment.url}password/reset`);
|
||||
expect(request.request.method).toBe('POST');
|
||||
expect(request.request.body).toEqual(payload);
|
||||
request.flush(
|
||||
{
|
||||
message: 'Contraseña modificada correctamente.',
|
||||
status: 'used'
|
||||
},
|
||||
{ status: 200, statusText: 'OK' }
|
||||
);
|
||||
|
||||
httpController.verify();
|
||||
});
|
||||
|
||||
it('propagates logout errors without clearing the local session', () => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { DOCUMENT } from '@angular/common';
|
||||
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
|
||||
import { HttpClient, HttpErrorResponse, HttpResponse } from '@angular/common/http';
|
||||
import { isPlatformBrowser, isPlatformServer } from '@angular/common';
|
||||
import { computed, inject, Injectable, PLATFORM_ID, signal, TransferState, makeStateKey } from '@angular/core';
|
||||
import { firstValueFrom, map, Observable, of, tap } from 'rxjs';
|
||||
@@ -11,7 +11,11 @@ import {
|
||||
LoginResponse,
|
||||
RegisterPayload,
|
||||
RegisterResponse,
|
||||
UpdateProfilePayload
|
||||
ResetPasswordPayload,
|
||||
ResetPasswordAttemptResponse,
|
||||
ResetPasswordResponse,
|
||||
UpdateProfilePayload,
|
||||
ValidateResetPasswordAttemptResponse
|
||||
} from './auth.interfaces';
|
||||
import { CookieService } from '../cookie/cookie.service';
|
||||
import { TenantService } from '../tenant.service';
|
||||
@@ -53,6 +57,42 @@ export class AuthService {
|
||||
});
|
||||
}
|
||||
|
||||
requestPasswordReset(
|
||||
email: string
|
||||
): Observable<HttpResponse<ResetPasswordAttemptResponse>> {
|
||||
const tenantCode = this.tenantService.getTenant()?.codigo;
|
||||
|
||||
return this.http.post<ResetPasswordAttemptResponse>(
|
||||
`${environment.url}password/reset-attempts`,
|
||||
{
|
||||
email,
|
||||
...(tenantCode ? { tenant_codigo: tenantCode } : {})
|
||||
},
|
||||
{ observe: 'response' }
|
||||
);
|
||||
}
|
||||
|
||||
validatePasswordResetCode(
|
||||
email: string,
|
||||
codigo: string
|
||||
): Observable<HttpResponse<ValidateResetPasswordAttemptResponse>> {
|
||||
return this.http.post<ValidateResetPasswordAttemptResponse>(
|
||||
`${environment.url}password/reset-attempts/validate`,
|
||||
{ email, codigo },
|
||||
{ observe: 'response' }
|
||||
);
|
||||
}
|
||||
|
||||
resetPassword(
|
||||
payload: ResetPasswordPayload
|
||||
): Observable<HttpResponse<ResetPasswordResponse>> {
|
||||
return this.http.post<ResetPasswordResponse>(
|
||||
`${environment.url}password/reset`,
|
||||
payload,
|
||||
{ observe: 'response' }
|
||||
);
|
||||
}
|
||||
|
||||
loginWithGoogle(): void {
|
||||
const tenant = this.tenantService.getTenant();
|
||||
if (!tenant) {
|
||||
|
||||
Reference in New Issue
Block a user