aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/auth/Locked.tsx
blob: 34d010782d2b9996d5232e0a07a8662bde5bf67d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { systemPreferences } from '@electron/remote';
import { Component } from 'react';
import { observer } from 'mobx-react';
import { defineMessages, injectIntl, WrappedComponentProps } from 'react-intl';
import { noop } from 'lodash';
import Form from '../../lib/Form';
import Input from '../ui/input/index';
import Button from '../ui/button';
import { H1 } from '../ui/headline';
import { isMac } from '../../environment';

const messages = defineMessages({
  headline: {
    id: 'locked.headline',
    defaultMessage: 'Locked',
  },
  touchId: {
    id: 'locked.touchId',
    defaultMessage: 'Unlock with Touch ID',
  },
  passwordLabel: {
    id: 'locked.password.label',
    defaultMessage: 'Password',
  },
  submitButtonLabel: {
    id: 'locked.submit.label',
    defaultMessage: 'Unlock',
  },
  unlockWithPassword: {
    id: 'locked.unlockWithPassword',
    defaultMessage: 'Unlock with Password',
  },
  invalidCredentials: {
    id: 'locked.invalidCredentials',
    defaultMessage: 'Password invalid',
  },
});

interface IProps extends WrappedComponentProps {
  onSubmit: (...args: any[]) => void;
  unlock: () => void;
  isSubmitting: boolean;
  useTouchIdToUnlock: boolean;
  error: boolean;
}

@observer
class Locked extends Component<IProps> {
  form: Form;

  constructor(props: IProps) {
    super(props);

    this.form = new Form({
      fields: {
        password: {
          label: this.props.intl.formatMessage(messages.passwordLabel),
          value: '',
          type: 'password',
        },
      },
    });
  }

  submit(e) {
    e.preventDefault();
    this.form.submit({
      onSuccess: form => {
        this.props.onSubmit(form.values());
      },
      onError: noop,
    });
  }

  touchIdUnlock() {
    const { intl } = this.props;

    systemPreferences
      .promptTouchID(intl.formatMessage(messages.touchId))
      .then(() => {
        this.props.unlock();
      });
  }

  render() {
    const { form } = this;
    const { isSubmitting, error, useTouchIdToUnlock, intl } = this.props;

    const touchIdEnabled = isMac
      ? useTouchIdToUnlock && systemPreferences.canPromptTouchID()
      : false;
    const submitButtonLabel = touchIdEnabled
      ? intl.formatMessage(messages.unlockWithPassword)
      : intl.formatMessage(messages.submitButtonLabel);

    return (
      <div className="auth__container">
        <form className="franz-form auth__form" onSubmit={e => this.submit(e)}>
          <img src="./assets/images/logo.svg" className="auth__logo" alt="" />
          <H1>{intl.formatMessage(messages.headline)}</H1>

          {touchIdEnabled && (
            <>
              <Button
                className="auth__button touchid__button"
                label={intl.formatMessage(messages.touchId)}
                onClick={() => this.touchIdUnlock()}
                type="button"
              />
              <hr className="locked__or_line" />
            </>
          )}

          <Input {...form.$('password').bind()} showPasswordToggle focus />
          {error && (
            <p className="error-message center">
              {intl.formatMessage(messages.invalidCredentials)}
            </p>
          )}
          {isSubmitting ? (
            <Button
              className="auth__button is-loading"
              buttonType="secondary"
              label={`${submitButtonLabel} ...`}
              loaded={false}
              onClick={noop}
              disabled
            />
          ) : (
            <Button
              type="submit"
              className="auth__button"
              label={submitButtonLabel}
              onClick={noop}
            />
          )}
        </form>
      </div>
    );
  }
}

export default injectIntl(Locked);