All files / web/modules/auth/components Login.tsx

29.79% Statements 14/47
0% Branches 0/2
0% Functions 0/4
34.15% Lines 14/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1202x 1x   1x 1x 1x 1x 1x 1x 1x 1x         1x 1x                       1x                                                                                                                                                                     1x                
import Button from 'antd/lib/button';
import Form from 'antd/lib/form';
import { FormComponentProps } from 'antd/lib/form/Form';
import Icon from 'antd/lib/icon';
import Input from 'antd/lib/input';
import message from 'antd/lib/message';
import * as React from 'react';
import { connect } from 'react-redux';
import { useBoolean } from '../../../customHooks';
import Link from '../../components/Link';
import {
  authSetAuthDataAction,
  authSetAuthStateAction,
  authSetIsLoadingAction,
} from '../actions';
import { currentAuthenticatedUser, signIn } from '../api';
import {
  AUTH_STATE_AUTHENTICATED,
  AUTH_STATE_FORGOT_PASSWORD,
  AUTH_STATE_SET_NEW_PASSWORD,
} from '../constants';
 
interface IProps extends FormComponentProps {
  setAuthState: any;
  setAuthData: any;
  setIsLoading: any;
}
 
const Login = ({ form, setAuthState, setAuthData, setIsLoading }: IProps) => {
  const isLoading = useBoolean(false);
 
  const handleSubmit = (e: any) => {
    e.preventDefault();
    form.validateFields(async (err: any, values: any) => {
      if (!err) {
        try {
          isLoading.setTrue();
          await setIsLoading(true);
 
          const data = await signIn(values.username, values.password);
          await setAuthData(data);
 
          if ('challengeName' in data) {
            const { challengeName } = data;
            if (challengeName === 'NEW_PASSWORD_REQUIRED') {
              await setAuthState(AUTH_STATE_SET_NEW_PASSWORD);
            }
          } else {
            const authData = await currentAuthenticatedUser();
            await setAuthData(authData);
            await setAuthState(AUTH_STATE_AUTHENTICATED);
            message.success('Úspěšné přihlášení');
            await setIsLoading(false);
          }
        } catch (e) {
          if (
            e.code === 'NotAuthorizedException' ||
            e.code === 'UserNotFoundException'
          ) {
            message.info('Chybné uživatelské jméno nebo heslo');
          } else {
            message.error(e.message);
          }
          isLoading.setFalse();
          await setIsLoading(false);
        }
      }
    });
  };
 
  const handleSetAuthStateForgot = () => {
    setAuthState(AUTH_STATE_FORGOT_PASSWORD);
  };
 
  const { getFieldDecorator } = form;
  return (
    <Form onSubmit={handleSubmit}>
      <Form.Item>
        {getFieldDecorator('username', {
          rules: [
            { required: true, message: 'Prosím zadejte uživatelské jméno!' },
          ],
        })(
          <Input
            prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />}
            placeholder="Uživatelské jméno"
          />,
        )}
      </Form.Item>
      <Form.Item>
        {getFieldDecorator('password', {
          rules: [{ required: true, message: 'Prosím zadejte heslo!' }],
        })(
          <Input.Password
            prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />}
            placeholder="Heslo"
          />,
        )}
      </Form.Item>
      <Form.Item>
        <Button type="primary" htmlType="submit" loading={isLoading.value}>
          Přihlásit
        </Button>
      </Form.Item>
      <Form.Item>
        <Link onClick={handleSetAuthStateForgot}>Zapomenuté heslo</Link>
      </Form.Item>
    </Form>
  );
};
 
export default connect(
  () => ({}),
  {
    setAuthState: authSetAuthStateAction,
    setAuthData: authSetAuthDataAction,
    setIsLoading: authSetIsLoadingAction,
  },
)(Form.create()(Login));