|
1 | | -import { Component, Input, OnInit } from '@angular/core'; |
2 | | -import { ActivatedRoute, Router } from '@angular/router'; |
3 | | -import { AccountService } from 'src/app/services/account.service'; |
4 | | -import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal'; |
5 | | -import { User } from 'src/app/interfaces/user'; |
6 | | -import { NotificationService } from '../services/notification.service'; |
| 1 | +import { Component, OnInit } from '@angular/core'; |
| 2 | +import { ActivatedRoute } from '@angular/router'; |
| 3 | +import { HttpClient } from '@angular/common/http'; |
7 | 4 |
|
8 | 5 | @Component({ |
9 | 6 | selector: 'app-email-confirmation', |
10 | 7 | templateUrl: './email-confirmation.component.html', |
11 | | - styleUrls: ['./email-confirmation.component.css'] |
| 8 | + styleUrls: ['./email-confirmation.component.scss'] |
12 | 9 | }) |
13 | 10 | export class EmailConfirmationComponent implements OnInit { |
| 11 | + |
| 12 | + userId: string | null = null; |
| 13 | + token: string | null = null; |
14 | 14 |
|
15 | | - result: string = ""; |
16 | | - |
17 | | - constructor( |
18 | | - private router: Router, |
19 | | - private route: ActivatedRoute, |
20 | | - private accountService: AccountService, |
21 | | - private modalService: BsModalService, |
22 | | - public notfi: NotificationService |
23 | | - ) { |
24 | | - |
25 | | - } |
26 | | - |
27 | | - async ngOnInit(): Promise<void> { |
28 | | - this.route.queryParams.subscribe(params=>{ |
29 | | - const res = params['result']; |
30 | | - const checkIfSuccess = res === "true"; |
31 | | - if(checkIfSuccess) |
32 | | - { |
33 | | - this.router.navigate(['/login']); |
34 | | - this.notfi.showInfo('Email Confirmation Successful','You can now login!'); |
35 | | - CheckEmailToken: Boolean = null; |
| 15 | + confirming = false; |
| 16 | + confirmed = false; |
| 17 | + error = false; |
| 18 | + linkInvalid = false; |
| 19 | + showRegister = false; |
| 20 | + |
| 21 | + message: string = "Loading confirmation details..."; |
| 22 | + |
| 23 | + constructor(private route: ActivatedRoute, private http: HttpClient) {} |
| 24 | + |
| 25 | + ngOnInit(): void { |
| 26 | + this.route.queryParams.subscribe(params => { |
| 27 | + const uid = params['userid']; |
| 28 | + const tok = params['token']; |
| 29 | + |
| 30 | + this.userId = uid ?? null; |
| 31 | + this.token = tok ?? null; |
| 32 | + |
| 33 | + if (!this.userId || !this.token) { |
| 34 | + this.message = "Invalid confirmation link. Missing user id or token."; |
| 35 | + return; |
36 | 36 | } |
37 | | - else{ |
38 | | - this.result = res; |
| 37 | + |
| 38 | + this.message = "Click below to confirm your email."; |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + onConfirmClick(): void { |
| 43 | + if (!this.userId || !this.token) { |
| 44 | + this.message = "Invalid confirmation link. Missing user id or token."; |
| 45 | + this.error = true; |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + this.confirming = true; |
| 50 | + this.error = false; |
| 51 | + |
| 52 | + const url = `/api/Account/ConfirmEmailPost?userID=${encodeURIComponent(this.userId)}&token=${encodeURIComponent(this.token)}`; |
| 53 | + |
| 54 | + this.http.post<any>(url, {}).subscribe({ |
| 55 | + next: (res) => { |
| 56 | + this.confirming = false; |
| 57 | + |
| 58 | + const msg = res?.message ?? "Email confirmation complete."; |
| 59 | + this.message = msg; |
| 60 | + |
| 61 | + // Treat already verified as confirmed state |
| 62 | + if (msg.toLowerCase().includes("already")) { |
| 63 | + this.confirmed = true; |
| 64 | + } else if (res?.success) { |
| 65 | + this.confirmed = true; |
| 66 | + } |
| 67 | + }, |
| 68 | + error: (err) => { |
| 69 | + this.confirming = false; |
| 70 | + this.error = true; |
| 71 | + |
| 72 | + // Default message |
| 73 | + let msg = "Account confirmation failed. Please try again later."; |
| 74 | + |
| 75 | + // If server returns JSON message |
| 76 | + const serverMsg = err?.error?.message; |
| 77 | + if (typeof serverMsg === "string" && serverMsg.trim().length > 0) { |
| 78 | + msg = serverMsg; |
| 79 | + } |
| 80 | + |
| 81 | + // If server returns 500+ |
| 82 | + if (err?.status >= 500) { |
| 83 | + msg = "Something went wrong while confirming your email. Please try again later."; |
| 84 | + } |
| 85 | + |
| 86 | + this.message = msg; |
| 87 | + |
| 88 | + // Detect invalid link / user not found cases |
| 89 | + const msgLower = serverMsg.toLowerCase(); |
| 90 | + |
| 91 | + // User does not exist |
| 92 | + if (msgLower.includes("not been registered")) { |
| 93 | + this.linkInvalid = true; |
| 94 | + this.showRegister = true; |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + // Invalid / expired token |
| 99 | + if ( |
| 100 | + msgLower.includes("invalid") || |
| 101 | + msgLower.includes("expired") |
| 102 | + ) { |
| 103 | + this.linkInvalid = true; |
| 104 | + } |
39 | 105 | } |
40 | | - }) |
| 106 | + }); |
41 | 107 | } |
42 | 108 | } |
0 commit comments