DEV: Convert second-factor-input to gjs (#25946)

This commit is contained in:
Jarek Radosz 2024-02-29 12:27:12 +01:00 committed by GitHub
parent 5c54fbfdb1
commit 49e67d32fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 86 additions and 90 deletions

View File

@ -87,11 +87,11 @@
/>
{{else}}
<SecondFactorInput
@value={{@secondFactorToken}}
@inputId="login-second-factor"
@secondFactorMethod={{@secondFactorMethod}}
@backupEnabled={{@backupEnabled}}
{{on "keydown" this.loginOnEnter}}
{{on "input" (with-event-value (fn (mut @secondFactorToken)))}}
@secondFactorMethod={{@secondFactorMethod}}
value={{@secondFactorToken}}
id="login-second-factor"
/>
{{/if}}
</SecondFactorForm>

View File

@ -45,27 +45,26 @@
"user.second_factor.name"
}}</label>
<div class="controls">
<SecondFactorInput
<input
{{on "input" (with-event-value (fn (mut this.secondFactorName)))}}
value={{this.secondFactorName}}
type="text"
placeholder={{i18n "user.second_factor.totp.default_name"}}
maxlength={{this.maxSecondFactorNameLength}}
@value={{this.secondFactorName}}
@inputId="second-factor-name"
@placeholder={{i18n "user.second_factor.totp.default_name"}}
id="second-factor-name"
/>
</div>
<label class="control-label input-prepend">{{i18n
"user.second_factor.label"
}}</label>
<label class="control-label input-prepend">
{{i18n "user.second_factor.label"}}
</label>
<div class="controls">
<TextField
class="second-factor-token-input"
maxlength={{6}}
@value={{this.secondFactorToken}}
@inputId="second-factor-token"
<SecondFactorInput
{{on "input" (with-event-value (fn (mut this.secondFactorToken)))}}
@secondFactorMethod={{this.totpType}}
value={{this.secondFactorToken}}
placeholder="123456"
autocorrect="off"
autocapitalize="off"
autofocus="autofocus"
id="second-factor-token"
/>
</div>
</div>

View File

@ -1,7 +1,10 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
import { MAX_SECOND_FACTOR_NAME_LENGTH } from "discourse/models/user";
import {
MAX_SECOND_FACTOR_NAME_LENGTH,
SECOND_FACTOR_METHODS,
} from "discourse/models/user";
import I18n from "discourse-i18n";
export default class SecondFactorAddTotp extends Component {
@ -13,6 +16,7 @@ export default class SecondFactorAddTotp extends Component {
@tracked secondFactorToken;
maxSecondFactorNameLength = MAX_SECOND_FACTOR_NAME_LENGTH;
totpType = SECOND_FACTOR_METHODS.TOTP;
@action
totpRequested() {

View File

@ -0,0 +1,50 @@
import Component from "@glimmer/component";
import { SECOND_FACTOR_METHODS } from "discourse/models/user";
export default class SecondFactorInput extends Component {
get isTotp() {
return this.args.secondFactorMethod === SECOND_FACTOR_METHODS.TOTP;
}
get isBackupCode() {
return this.args.secondFactorMethod === SECOND_FACTOR_METHODS.BACKUP_CODE;
}
get type() {
if (this.isTotp) {
return "tel";
} else if (this.isBackupCode) {
return "text";
}
}
get pattern() {
if (this.isTotp) {
return "[0-9]{6}";
} else if (this.isBackupCode) {
return "[a-z0-9]{16}";
}
}
get maxlength() {
if (this.isTotp) {
return "6";
} else if (this.isBackupCode) {
return "32";
}
}
<template>
<input
type={{this.type}}
pattern={{this.pattern}}
maxlength={{this.maxlength}}
autocomplete="one-time-code"
autocapitalize="off"
autocorrect="off"
autofocus="autofocus"
class="second-factor-token-input"
...attributes
/>
</template>
}

View File

@ -1,15 +0,0 @@
<TextField
@value={{this.value}}
@type={{this.type}}
@input={{action "onInput"}}
@placeholder={{this.placeholder}}
@autocomplete="one-time-code"
pattern={{this.pattern}}
maxlength={{this.maxlength}}
class="second-factor-token-input"
id={{this.inputId}}
autocapitalize="off"
autocorrect="off"
autofocus="autofocus"
...attributes
/>

View File

@ -1,43 +0,0 @@
import Component from "@ember/component";
import { action } from "@ember/object";
import { SECOND_FACTOR_METHODS } from "discourse/models/user";
import discourseComputed from "discourse-common/utils/decorators";
export default Component.extend({
@discourseComputed("secondFactorMethod")
type(secondFactorMethod) {
if (secondFactorMethod === SECOND_FACTOR_METHODS.TOTP) {
return "tel";
}
if (secondFactorMethod === SECOND_FACTOR_METHODS.BACKUP_CODE) {
return "text";
}
},
@discourseComputed("secondFactorMethod")
pattern(secondFactorMethod) {
if (secondFactorMethod === SECOND_FACTOR_METHODS.TOTP) {
return "[0-9]{6}";
}
if (secondFactorMethod === SECOND_FACTOR_METHODS.BACKUP_CODE) {
return "[a-z0-9]{16}";
}
},
@discourseComputed("secondFactorMethod")
maxlength(secondFactorMethod) {
if (secondFactorMethod === SECOND_FACTOR_METHODS.TOTP) {
return "6";
}
if (secondFactorMethod === SECOND_FACTOR_METHODS.BACKUP_CODE) {
return "32";
}
},
@action
onInput() {
if (this.onTokenInput) {
this.onTokenInput(...arguments);
}
},
});

View File

@ -209,11 +209,6 @@ export default Controller.extend({
});
},
@action
onTokenInput(event) {
this.set("secondFactorToken", event.target.value);
},
@action
useAnotherMethod(newMethod, event) {
event?.preventDefault();

View File

@ -32,9 +32,12 @@
@isLogin={{true}}
>
<SecondFactorInput
@value={{this.secondFactorToken}}
{{on
"input"
(with-event-value (fn (mut this.secondFactorToken)))
}}
@secondFactorMethod={{this.secondFactorMethod}}
@backupEnabled={{this.backupEnabled}}
value={{this.secondFactorToken}}
/>
</SecondFactorForm>
{{/if}}

View File

@ -43,10 +43,13 @@
@isLogin={{false}}
>
<SecondFactorInput
@value={{this.secondFactorToken}}
@inputId="second-factor"
{{on
"input"
(with-event-value (fn (mut this.secondFactorToken)))
}}
@secondFactorMethod={{this.secondFactorMethod}}
@backupEnabled={{this.backupEnabled}}
value={{this.secondFactorToken}}
id="second-factor"
/>
</SecondFactorForm>
{{/if}}

View File

@ -20,9 +20,9 @@
{{else if (or this.showTotpForm this.showBackupCodesForm)}}
<form class={{this.inputFormClass}}>
<SecondFactorInput
@value={{this.secondFactorToken}}
{{on "input" (with-event-value (fn (mut this.secondFactorToken)))}}
@secondFactorMethod={{this.shownSecondFactorMethod}}
@onTokenInput={{action "onTokenInput"}}
value={{this.secondFactorToken}}
/>
<DButton
@action={{this.authenticateToken}}