aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/basicAuth/Component.js
blob: acba5a90dcb8ad5955ca6cfe814127fa8438c268 (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
import { Component } from 'react';
import PropTypes from 'prop-types';
import injectSheet from 'react-jss';
import { observer } from 'mobx-react';
import { defineMessages, injectIntl } from 'react-intl';
import classnames from 'classnames';

import Modal from '../../components/ui/Modal';
import Input from '../../components/ui/Input';
import Button from '../../components/ui/button';

import { state, resetState, sendCredentials, cancelLogin } from './store';
import Form from './Form';

import styles from './styles';
import globalMessages from '../../i18n/globalMessages';
import { H1 } from '../../components/ui/headline';

const messages = defineMessages({
  signIn: {
    id: 'feature.basicAuth.signIn',
    defaultMessage: 'Sign In',
  },
});

class BasicAuthModal extends Component {
  static propTypes = {
    classes: PropTypes.object.isRequired,
  };

  submit(e) {
    e.preventDefault();

    const values = Form.values();

    sendCredentials(values.user, values.password);
    resetState();
  }

  cancel() {
    cancelLogin();
    this.close();
  }

  close() {
    resetState();
    state.isModalVisible = false;
  }

  render() {
    const { classes } = this.props;

    const { isModalVisible, authInfo } = state;

    if (!authInfo) {
      return null;
    }

    const { intl } = this.props;

    return (
      <Modal
        isOpen={isModalVisible}
        className={classes.modal}
        close={this.cancel.bind(this)}
        showClose={false}
      >
        <H1>{intl.formatMessage(messages.signIn)}</H1>
        <p>
          http
          {authInfo.port === 443 && 's'}
          ://
          {authInfo.host}
        </p>
        <form
          onSubmit={this.submit.bind(this)}
          className={classnames('franz-form', classes.form)}
        >
          <Input field={Form.$('user')} showLabel={false} />
          <Input
            field={Form.$('password')}
            showLabel={false}
            showPasswordToggle
          />
          <div className={classes.buttons}>
            <Button
              type="button"
              label={intl.formatMessage(globalMessages.cancel)}
              buttonType="secondary"
              onClick={this.cancel.bind(this)}
            />
            <Button type="submit" label={intl.formatMessage(messages.signIn)} />
          </div>
        </form>
      </Modal>
    );
  }
}
export default injectIntl(
  injectSheet(styles, { injectTheme: true })(observer(BasicAuthModal)),
);