{"size": 2429, "ext": "js", "max_stars_repo_path": "js/src/forum/components/TwoFactorLogInModal.js", "max_stars_repo_name": "ZeQueen-X/flarum-ext-twofactor", "max_stars_repo_stars_event_min_datetime": "2021-07-08T20:41:13.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-08T20:41:13.000Z", "max_issues_repo_path": "js/src/forum/components/TwoFactorLogInModal.js", "max_issues_repo_name": "ZeQueen-X/flarum-ext-twofactor", "max_issues_repo_issues_event_min_datetime": "2021-05-17T15:36:05.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-02T13:28:27.000Z", "max_forks_repo_path": "js/src/forum/components/TwoFactorLogInModal.js", "max_forks_repo_name": "ZeQueen-X/flarum-ext-twofactor", "max_forks_repo_forks_event_min_datetime": "2021-05-16T21:09:27.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-15T19:04:24.000Z"}
{"max_stars_count": 1, "max_issues_count": 6, "max_forks_count": 3, "avg_line_length": 29.265060241, "max_line_length": 108, "alphanum_fraction": 0.4606834088}
import Button from 'flarum/common/components/Button';
import Modal from 'flarum/common/components/Modal';
import Stream from 'flarum/common/utils/Stream';

const trans = key => {
    return app.translator.trans(`nearata-twofactor.forum.${key}`);
};

export default class TwoFactorLogInModal extends Modal {
    oninit(vnode) {
        super.oninit(vnode);

        const { identification, password, remember } = this.attrs;

        this.identification = identification;
        this.password = password;
        this.remember = remember;

        this.code = Stream('');
    }

    className() {
        return 'TwoFactorLogInModal Modal--small';
    }

    title() {
        return trans('log_in_modal.title');
    }

    content() {
        return [
            m('.Modal-body', [
                m('.Form.Form--centered', [
                    m('.Form-group', [
                        m('input', {
                            class: 'FormControl',
                            type: 'text',
                            placeholder: trans('log_in_modal.otp_placeholder'),
                            name: 'otp',
                            autocomplete: 'off',
                            bidi: this.code,
                            disabled: this.loading
                        })
                    ]),
                    m('.Form-group', [
                        Button.component(
                            {
                                className: 'Button Button--primary Button--block',
                                type: 'submit',
                                loading: this.loading,
                            },
                            trans('log_in_modal.submit_button')
                        )
                    ])
                ])
            ])
        ];
    }

    onsubmit(e) {
        e.preventDefault();

        this.loading = true;

        const identification = this.identification;
        const password = this.password;
        const remember = this.remember;
        const twofa = this.code;

        app.session
            .login({ identification, password, remember, twofa }, { errorHandler: this.onerror.bind(this) })
            .then(() => window.location.reload(), this.loaded.bind(this));
    }

    onerror(error) {
        if (error.status === 401) {
            error.alert.content = trans('invalid_twofa_code');
        }

        super.onerror(error);
    }
}
