import { Component, computed, inject, signal } from '@angular/core'; import { RouterLink } from '@angular/router'; import { firstValueFrom } from 'rxjs'; import { AuthTokenService, UserService } from '@africanies/africanies-core'; import { AlertComponent, ButtonComponent, TextInputComponent, } from '@africanies/africanies-ui'; import { PageHeaderComponent } from '../../shared/page-header.component'; import { playgroundErrorMessage } from '../../shared/playground-notify'; /** * Product `/onboarding/reset-password` — first login with a default password. * Not the email-link forgot-password flow (no token in this route). */ @Component({ selector: 'app-onboarding-reset-password-page', standalone: true, imports: [ RouterLink, AlertComponent, ButtonComponent, TextInputComponent, PageHeaderComponent, ], template: `
@if (errorMessage(); as err) { } @if (successMessage(); as ok) { } @if (!hasToken()) { } Back to login
`, }) export class OnboardingResetPasswordPage { private readonly users = inject(UserService); private readonly auth = inject(AuthTokenService); protected readonly currentPassword = signal(''); protected readonly password = signal(''); protected readonly confirmation = signal(''); protected readonly saving = signal(false); protected readonly errorMessage = signal(null); protected readonly successMessage = signal(null); protected readonly hasToken = computed(() => this.auth.token() != null); protected readonly confirmError = computed(() => { const next = this.password(); const confirm = this.confirmation(); if (!confirm || next === confirm) { return null; } return 'Passwords do not match.'; }); protected readonly canSubmit = computed(() => { const current = this.currentPassword().trim(); const next = this.password().trim(); const confirm = this.confirmation().trim(); return ( this.hasToken() && !this.saving() && current !== '' && next !== '' && next === confirm ); }); protected async save(): Promise { if (!this.canSubmit()) { return; } this.saving.set(true); this.errorMessage.set(null); this.successMessage.set(null); try { const res = await firstValueFrom( this.users.changePassword({ current_password: this.currentPassword(), password: this.password(), password_confirmation: this.confirmation(), }), ); if (res.success) { this.successMessage.set( res.message?.trim() || 'Password updated. Continue to the dashboard.', ); return; } this.errorMessage.set(res.message?.trim() || 'Could not update password.'); } catch (err) { this.errorMessage.set( playgroundErrorMessage(err, { suggestApiToken: true }), ); } finally { this.saving.set(false); } } }