aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/auth/Login.tsx
blob: 37ce595eb86fb456514b674eab51caf7dfa02cca (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { mdiArrowLeftCircle } from '@mdi/js';
import { noop } from 'lodash';
import { observer } from 'mobx-react';
import { Component, type FormEvent, type ReactElement } from 'react';
import {
  type WrappedComponentProps,
  defineMessages,
  injectIntl,
} from 'react-intl';
import type { GlobalError } from '../../@types/ferdium-components.types';
import { serverBase } from '../../api/apiBase'; // TODO: Remove this line after fixing password recovery in-app
import { LIVE_FRANZ_API } from '../../config';
import { API_VERSION } from '../../environment-remote';
import { email, required } from '../../helpers/validation-helpers';
import Form from '../../lib/Form';
import Link from '../ui/Link';
import Button from '../ui/button';
import { H1 } from '../ui/headline';
import Icon from '../ui/icon';
import Input from '../ui/input/index';

const messages = defineMessages({
  headline: {
    id: 'login.headline',
    defaultMessage: 'Sign in',
  },
  emailLabel: {
    id: 'login.email.label',
    defaultMessage: 'Email address',
  },
  passwordLabel: {
    id: 'login.password.label',
    defaultMessage: 'Password',
  },
  submitButtonLabel: {
    id: 'login.submit.label',
    defaultMessage: 'Sign in',
  },
  invalidCredentials: {
    id: 'login.invalidCredentials',
    defaultMessage: 'Email or password not valid',
  },
  customServerQuestion: {
    id: 'login.customServerQuestion',
    defaultMessage: 'Using a custom Ferdium server?',
  },
  customServerSuggestion: {
    id: 'login.customServerSuggestion',
    defaultMessage: 'Try importing your Franz account',
  },
  tokenExpired: {
    id: 'login.tokenExpired',
    defaultMessage: 'Your session expired, please login again.',
  },
  serverLogout: {
    id: 'login.serverLogout',
    defaultMessage: 'Your session expired, please login again.',
  },
  signupLink: {
    id: 'login.link.signup',
    defaultMessage: 'Create a free account',
  },
  passwordLink: {
    id: 'login.link.password',
    defaultMessage: 'Reset password',
  },
});

interface IProps extends WrappedComponentProps {
  onSubmit: (...args: any[]) => void;
  isSubmitting: boolean;
  isTokenExpired: boolean;
  isServerLogout: boolean;
  signupRoute: string;
  passwordRoute: string; // TODO: Uncomment this line after fixing password recovery in-app
  error: GlobalError;
}

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

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

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

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

  render(): ReactElement {
    const { form } = this;
    const {
      isSubmitting,
      isTokenExpired,
      isServerLogout,
      signupRoute,
      error,
      intl,
      // passwordRoute, // TODO: Uncomment this line after fixing password recovery in-app
    } = this.props;

    return (
      <div className="auth__container">
        <form className="franz-form auth__form" onSubmit={e => this.submit(e)}>
          <Link to="/auth/welcome">
            <img src="./assets/images/logo.svg" className="auth__logo" alt="" />
          </Link>
          <H1>{intl.formatMessage(messages.headline)}</H1>
          {isTokenExpired && (
            <p className="error-message center">
              {intl.formatMessage(messages.tokenExpired)}
            </p>
          )}
          {isServerLogout && (
            <p className="error-message center">
              {intl.formatMessage(messages.serverLogout)}
            </p>
          )}
          <Input {...form.$('email').bind()} focus />
          <Input {...form.$('password').bind()} showPasswordToggle />
          {error.code === 'invalid-credentials' && (
            <>
              <h2 className="error-message center">
                {intl.formatMessage(messages.invalidCredentials)}
              </h2>
              {window['ferdium'].stores.settings.all.app.server !==
                LIVE_FRANZ_API && (
                <>
                  <p className="error-message center">
                    {intl.formatMessage(messages.customServerQuestion)}{' '}
                  </p>
                  <p className="error-message center">
                    <Link
                      to={`${window[
                        'ferdium'
                      ].stores.settings.all.app.server.replace(
                        API_VERSION,
                        '',
                      )}/import`}
                      target="_blank"
                      style={{ cursor: 'pointer', textDecoration: 'underline' }}
                    >
                      {intl.formatMessage(messages.customServerSuggestion)}
                    </Link>
                  </p>
                </>
              )}
            </>
          )}
          {isSubmitting ? (
            <Button
              className="auth__button is-loading"
              buttonType="secondary"
              label={`${intl.formatMessage(messages.submitButtonLabel)} ...`}
              loaded={false}
              disabled
              onClick={noop}
            />
          ) : (
            <Button
              type="submit"
              className="auth__button"
              label={intl.formatMessage(messages.submitButtonLabel)}
              onClick={noop}
            />
          )}
        </form>
        <div className="auth__links">
          <Link to={signupRoute}>
            {intl.formatMessage(messages.signupLink)}
          </Link>
          <Link
            // to={passwordRoute} // TODO: Uncomment this line after fixing password recovery in-app
            to={`${serverBase()}/user/forgot`} // TODO: Remove this line after fixing password recovery in-app
            target="_blank" // TODO: Remove this line after fixing password recovery in-app
          >
            {intl.formatMessage(messages.passwordLink)}
          </Link>
        </div>
        <div className="auth__help">
          <Link to="/auth/welcome">
            <Icon icon={mdiArrowLeftCircle} size={1.5} />
          </Link>
        </div>
      </div>
    );
  }
}

export default injectIntl(Login);