diff --git a/frontend/angular-clarity-master/src/app/modules/main/components/captcha/captcha.component.ts b/frontend/angular-clarity-master/src/app/modules/main/components/captcha/captcha.component.ts new file mode 100644 index 0000000..e704f73 --- /dev/null +++ b/frontend/angular-clarity-master/src/app/modules/main/components/captcha/captcha.component.ts @@ -0,0 +1,96 @@ +import { Component, OnInit, Input, forwardRef } from '@angular/core'; +import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; + +@Component({ + selector: 'app-captcha', + template: ` +
+ +
+
+
+ {{ captchaCode }} +
+ +
+ +
+ * Captcha code does not match. +
+
+ ✓ Captcha verified +
+
+
+ `, + providers: [ + { + provide: NG_VALUE_ACCESSOR, + useExisting: forwardRef(() => CaptchaComponent), + multi: true + } + ] +}) +export class CaptchaComponent implements OnInit, ControlValueAccessor { + @Input() label: string = 'Verification Code'; + + captchaCode: string = ''; + enteredValue: string = ''; + isValid: boolean = false; + + private onChange: (val: any) => void = () => {}; + private onTouched: () => void = () => {}; + + ngOnInit(): void { + this.generateCaptcha(); + } + + generateCaptcha(): void { + const chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'; + let code = ''; + for (let i = 0; i < 6; i++) { + code += chars.charAt(Math.floor(Math.random() * chars.length)); + } + this.captchaCode = code; + this.enteredValue = ''; + this.isValid = false; + this.onChange(''); + } + + onInput(): void { + this.onTouched(); + if (this.enteredValue && this.enteredValue.trim() === this.captchaCode) { + this.isValid = true; + this.onChange(this.enteredValue.trim()); + } else { + this.isValid = false; + this.onChange(''); + } + } + + writeValue(value: any): void { + if (!value) { + this.enteredValue = ''; + this.isValid = false; + } + } + + registerOnChange(fn: any): void { + this.onChange = fn; + } + + registerOnTouched(fn: any): void { + this.onTouched = fn; + } +} + diff --git a/frontend/angular-clarity-master/src/app/modules/main/main.module.ts b/frontend/angular-clarity-master/src/app/modules/main/main.module.ts index 34c8213..f5bad99 100644 --- a/frontend/angular-clarity-master/src/app/modules/main/main.module.ts +++ b/frontend/angular-clarity-master/src/app/modules/main/main.module.ts @@ -41,6 +41,7 @@ import { UserRegistrationComponent } from '../main/admin/user-registration/user- import { QRCodeModule } from 'angularx-qrcode'; import { BarcodeDirective } from './directives/barcode.directive'; +import { CaptchaComponent } from './components/captcha/captcha.component'; import { TagInputModule } from 'ngx-chips'; import { CookieService } from 'ngx-cookie-service'; import { ImageCropperModule } from 'ngx-image-cropper'; @@ -123,6 +124,7 @@ import { DynamicFormViewerComponent } from './superadmin/dynamicform/dynamic-for @NgModule({ declarations: [ BarcodeDirective, + CaptchaComponent, MainPageComponent, PageNotFoundComponent, UserComponent, PasswordResetComponent, MyworkspaceComponent, ReportRunnerComponent, ReportrunnereditComponent, Reportrunneredit2Component, MenuGroupComponent, AllMenuGroupComponent, EditMenuGroupComponent, ReadOnlyMenuGroupComponent, UserRegistrationComponent,