import { Component, OnInit } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormBuilder, FormGroup, Validators, ReactiveFormsModule } from '@angular/forms'; import { ActivatedRoute, Router } from '@angular/router'; import { InputsModule } from '@progress/kendo-angular-inputs'; import { LabelModule } from '@progress/kendo-angular-label'; import { ButtonsModule } from '@progress/kendo-angular-buttons'; import { IndicatorsModule } from '@progress/kendo-angular-indicators'; import { AuthService } from '../../shared/services/auth.service'; import { passwordStrengthValidator, passwordMatchValidator, } from '../account/validators/password.validators'; type Step = 'loading' | 'invalid' | 'form'; @Component({ selector: 'app-accept-invitation', standalone: true, imports: [ CommonModule, ReactiveFormsModule, InputsModule, LabelModule, ButtonsModule, IndicatorsModule, ], template: `

River Of Life Christian Church

Checking your invitation…

This invitation can't be used

{{ invalidMessage }}

Welcome, {{ memberName }}. Set a password to finish creating your account and sign in.

Required. Must be at least 8 characters with an uppercase letter, a lowercase letter, a digit, and a special character. Required. Passwords do not match.

{{ errorMessage }}

`, }) export class AcceptInvitationComponent implements OnInit { step: Step = 'loading'; form: FormGroup; submitting = false; memberName: string | null = null; invalidMessage = 'This invitation link is invalid or has already been used.'; errorMessage = ''; private token = ''; constructor( private fb: FormBuilder, private auth: AuthService, private route: ActivatedRoute, private router: Router, ) { this.form = this.fb.group( { newPassword: ['', [Validators.required, passwordStrengthValidator()]], confirmPassword: ['', [Validators.required]], }, { validators: passwordMatchValidator() }, ); } ngOnInit(): void { this.token = this.route.snapshot.queryParamMap.get('token') ?? ''; if (!this.token) { this.step = 'invalid'; return; } this.auth.validateInvitation(this.token).subscribe({ next: (result) => { if (result.valid) { this.memberName = result.memberName ?? null; this.step = 'form'; } else { this.invalidMessage = result.expired ? 'This invitation link has expired. Please ask for a new one.' : 'This invitation link is invalid or has already been used.'; this.step = 'invalid'; } }, error: () => { this.step = 'invalid'; }, }); } onSubmit(): void { if (this.form.invalid) { this.form.markAllAsTouched(); return; } this.submitting = true; this.errorMessage = ''; this.auth.acceptInvitation(this.token, this.form.value.newPassword).subscribe({ next: () => { this.router.navigate(['/user-portal/dashboard']); }, error: (err) => { this.errorMessage = err.error?.message ?? 'Could not set your password. The link may have expired.'; this.submitting = false; }, }); } goToLogin(): void { this.router.navigate(['/login']); } }