File
Implements
Methods
ngOnDestroy
|
ngOnDestroy()
|
|
|
pasteAction
|
pasteAction(value: string)
|
|
Parameters :
Name |
Type |
Optional |
value |
string
|
No
|
|
Private
fb
|
Default value : inject(FormBuilder)
|
|
Protected
form
|
Default value : this.fb.group({
authKeyUri: new FormControl('', {
validators: [Validators.pattern(TOKEN_URI_REGEX)],
}),
})
|
|
Protected
token
|
Default value : output<any>({ alias: 'authKeyUri' })
|
|
import { Component, inject, OnDestroy, output } from '@angular/core';
import { FormBuilder, FormControl, Validators } from '@angular/forms';
import { TokenUriInputModule } from './token-uri-input.module';
import { PasteButtonComponent } from '../../../shared/components/paste-button/paste-button.component';
import { URI } from 'otpauth';
export const TOKEN_URI_REGEX =
/^otpauth:\/\/([ht]otp)\/(.+)\?([A-Z0-9.~_-]+=[^?&]*(?:&[A-Z0-9.~_-]+=[^?&]*)*)$/i;
@Component({
selector: 'app-token-uri-input',
templateUrl: './token-uri-input.component.html',
styleUrls: ['./token-uri-input.component.scss'],
standalone: true,
imports: [TokenUriInputModule, PasteButtonComponent],
})
export class TokenUriInputComponent implements OnDestroy {
private fb = inject(FormBuilder);
protected token = output<any>({ alias: 'authKeyUri' });
protected form = this.fb.group({
authKeyUri: new FormControl('', {
validators: [Validators.pattern(TOKEN_URI_REGEX)],
}),
});
constructor() {
this.form.valueChanges.subscribe(({ authKeyUri }) => {
this.form.markAllAsTouched();
if (this.form.valid) {
if (authKeyUri) {
try {
const token = URI.parse(authKeyUri);
this.token.emit({ ...token, secret: token.secret.base32 });
} catch (e) {
throw Error(`${e}`);
}
}
}
});
}
ngOnDestroy(): void {
this.form.reset({}, { emitEvent: false });
}
/**
*
* @param value
*/
pasteAction(value: string) {
this.form.patchValue({ authKeyUri: value });
}
}
<form [formGroup]="form">
<ion-list [inset]="true">
<ion-item button lines="none">
<ion-toggle color="secondary" #useAuthKeyInput>
<ion-label>Have an authenticator key?</ion-label>
<ion-note color="medium"
>Authenticator keys look like web addresses
</ion-note>
</ion-toggle>
</ion-item>
<ion-item *ngIf="useAuthKeyInput.checked" lines="none">
<ion-input
#authKeyUriInput
label="Key"
formControlName="authKeyUri"
[clearInput]="true"
placeholder="Paste authenticator key"
errorText="Invalid format"
>
<app-paste-button slot="end" (clipboard)="pasteAction($event)" />
</ion-input>
</ion-item>
</ion-list>
</form>
Legend
Html element with directive