This is a demo application to test and verify the ng-mtcaptcha library.
-
Clone the repository:
git clone https://github.com/mtcaptcha-public/mtcaptcha-angularjs-example.git cd mtcaptcha-angularjs-example -
Install dependencies:
npm install
-
Add your MTCaptcha sitekey:
- Open
src/app/app.ts - Replace the sitekey value with your actual MTCaptcha sitekey:
sitekey = 'YOUR_ACTUAL_SITEKEY';
- Open
# Using npm script (recommended)
npm start
# Or using Angular CLI directly
ng serve demo
# Or specify a custom port
ng serve demo --port 4200The application will be available at http://localhost:4200
The demo application includes 6 different examples showcasing various customization options:
- Standard Size (Default) - Basic configuration with standard widget size
- Mini Size - Compact widget perfect for smaller forms or mobile interfaces
- Custom Language Text - Customized button labels and messages
- Custom Styling - Custom CSS styles to match your application theme
- Mini with Custom Text - Combining mini size with custom language text
- Theme Configuration - Applying different themes to the captcha widget
The ng-mtcaptcha library provides a flexible API through the MTCaptchaComponent with various input properties and output events. Below is a comprehensive guide to all available parameters.
- Type:
string - Required: Yes
- Description: Your MTCaptcha sitekey (public key). Get your sitekey from MTCaptcha Dashboard
- Example:
sitekey = 'MTPublic-YourSiteKeyHere';
<ng-mtcaptcha [sitekey]="sitekey"></ng-mtcaptcha>
- Type:
'mini' | 'standard' - Default:
'standard' - Description: Controls the size of the captcha widget
'standard': Full-size widget (default)'mini': Compact widget for smaller spaces
- Example:
<!-- Standard size (default) --> <ng-mtcaptcha [sitekey]="sitekey" [widgetSize]="'standard'"></ng-mtcaptcha> <!-- Mini size --> <ng-mtcaptcha [sitekey]="sitekey" [widgetSize]="'mini'"></ng-mtcaptcha>
- Type:
string - Default: Browser default
- Description: Theme for the captcha widget. Common values include
'light','dark', etc. - Example:
<ng-mtcaptcha [sitekey]="sitekey" [theme]="'light'"></ng-mtcaptcha>
- Type:
Record<string, any> | string - Description: Customize language text and button labels. Can be provided as:
- A JSON object
- A JSON string
- Example (Object):
customLangText = { 'en': { 'verify': 'Verify Me', 'refresh': 'New Challenge', 'mandatory': 'Please complete the verification' } };
<ng-mtcaptcha [sitekey]="sitekey" [customLangText]="customLangText"> </ng-mtcaptcha>
- Example (String):
customLangText = '{"en":{"verify":"Verify","refresh":"Refresh"}}';
- Type:
Record<string, any> | string - Description: Apply custom CSS styles to the captcha widget. Can be provided as:
- A JSON object with CSS properties
- A JSON string
- Example (Object):
customStyle = { 'font-family': 'Arial, sans-serif', 'font-size': '16px', 'color': '#333333', 'border-radius': '8px' };
<ng-mtcaptcha [sitekey]="sitekey" [customStyle]="customStyle"> </ng-mtcaptcha>
- Example (String):
customStyle = '{"font-family":"Arial","font-size":"14px"}';
- Type:
EventEmitter<string> - Description: Emits the verification token when the captcha is successfully solved
- Usage:
<ng-mtcaptcha [sitekey]="sitekey" (token)="onToken($event)"> </ng-mtcaptcha>
onToken(token: string) { console.log('Captcha token:', token); // Send token to your backend for verification }
- Type:
EventEmitter<void> - Description: Emits when the captcha widget is fully rendered and ready
- Usage:
<ng-mtcaptcha [sitekey]="sitekey" (rendered)="onRendered()"> </ng-mtcaptcha>
onRendered() { console.log('Captcha is ready!'); }
- Type:
EventEmitter<void> - Description: Emits when the captcha verification token expires
- Usage:
<ng-mtcaptcha [sitekey]="sitekey" (expired)="onExpired()"> </ng-mtcaptcha>
onExpired() { console.log('Token expired, user needs to solve again'); }
- Type:
EventEmitter<any> - Description: Emits when the captcha encounters an error
- Usage:
<ng-mtcaptcha [sitekey]="sitekey" (error)="onError($event)"> </ng-mtcaptcha>
onError(error: any) { console.error('Captcha error:', error); }
import { Component } from '@angular/core';
import { MTCaptchaComponent } from 'ng-mtcaptcha';
@Component({
selector: 'app-example',
standalone: true,
imports: [MTCaptchaComponent],
template: `
<ng-mtcaptcha
[sitekey]="sitekey"
[widgetSize]="'standard'"
[theme]="'light'"
[customLangText]="customLangText"
[customStyle]="customStyle"
(rendered)="onRendered()"
(token)="onToken($event)"
(expired)="onExpired()"
(error)="onError($event)">
</ng-mtcaptcha>
`
})
export class ExampleComponent {
sitekey = 'MTPublic-YourSiteKeyHere';
customLangText = {
'en': {
'verify': 'Verify',
'refresh': 'Refresh'
}
};
customStyle = {
'font-family': 'Arial, sans-serif',
'font-size': '14px'
};
onRendered() {
console.log('Captcha rendered');
}
onToken(token: string) {
console.log('Token:', token);
}
onExpired() {
console.log('Token expired');
}
onError(error: any) {
console.error('Error:', error);
}
}The MTCaptchaService provides programmatic access to captcha functionality.
- Returns: The current verified token if captcha is solved,
nullotherwise - Usage:
constructor(private mtcaptchaService: MTCaptchaService) {} submitForm() { const token = this.mtcaptchaService.getVerifiedToken(); if (token) { // Token is available, proceed with form submission } else { // Show error, captcha not solved this.mtcaptchaService.showMandatory(); } }
- Description: Shows a mandatory error message if the captcha is not solved
- Usage:
onSubmit() { const token = this.mtcaptchaService.getVerifiedToken(); if (!token) { this.mtcaptchaService.showMandatory(); return; } // Proceed with submission }
- Description: Configure global MTCaptcha settings
- Usage:
ngOnInit() { this.mtcaptchaService.setGlobalConfig({ sitekey: this.sitekey, theme: 'light', widgetSize: 'standard', customLangText: { /* ... */ }, customStyle: { /* ... */ } }); }
- Description: Observable that emits tokens when captcha is verified
- Usage:
ngOnInit() { this.mtcaptchaService.token$.subscribe(token => { if (token) { console.log('Token received:', token); } }); }
- Description: Observable that emits when captcha is rendered
- Usage:
this.mtcaptchaService.rendered$.subscribe(() => { console.log('Captcha rendered'); });
- Description: Observable that emits when captcha is verified (includes token)
- Usage:
this.mtcaptchaService.verified$.subscribe(token => { console.log('Verified with token:', token); });
- Description: Observable that emits when captcha verification expires
- Usage:
this.mtcaptchaService.verifyexpired$.subscribe(() => { console.log('Verification expired'); });
- Description: Observable that emits when captcha encounters an error
- Usage:
this.mtcaptchaService.error$.subscribe(error => { console.error('Captcha error:', error); });
The demo application includes:
-
Multiple Captcha Examples
- Standard size widget
- Mini size widget
- Custom language text
- Custom styling
- Combined configurations
-
Event Handling
renderedevent - fires when captcha is renderedtokenevent - fires when captcha is solvederrorevent - fires if there's an errorexpiredevent - fires when token expires
-
Service Integration
- Service observables (
rendered$,error$,verifyexpired$) - Token retrieval via service
- Service observables (
-
Status Monitoring
- Real-time status display
- Token display when received
- Error messages if any
- All 6 captcha examples appear on page
- Standard size widget displays correctly
- Mini size widget displays correctly
- Custom text examples show customized labels
- Custom style examples apply custom CSS
-
renderedevent fires for each example - No error events (check browser console)
- Can interact with each captcha (click/verify)
-
tokenevent fires when solved (check token badges) - Tokens are displayed correctly for each example
- Check browser console for logs
- Verify MTCaptcha script loads (Network tab in DevTools)
- Test expiration (if applicable)
Open the browser console to see detailed logs:
- β Captcha rendered messages
- π« Token received messages
- β Error messages (if any)
- β° Expiration messages
In browser DevTools β Network tab, verify:
mtcaptcha.min.jsloads successfully (status 200)- No failed requests
- This demo application installs
ng-mtcaptchafrom npm (version ^0.5.0) - The library is available at: https://www.npmjs.com/package/ng-mtcaptcha
- Make sure to run
npm installbefore starting the application