# All files SigninForm.js

34.28% Statements 12/35
20% Branches 2/10
25% Functions 2/8
34.28% Lines 12/35

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
    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

    3x
    3x
    3x
    3x
    3x
    3x
    3x

    3x

    3x

    3x

    3x

    3x

     import React, { useState } from "react";
    import PropTypes from "prop-types";
    import axios from "axios";
    import styles from "./SigninForm.module.scss";
    import "antd/dist/antd.css";
    import { Form, Input, Button, Alert } from "antd";
    import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
    import { faArrowAltCircleRight } from "@fortawesome/free-regular-svg-icons";
    import "./signinform.css";
    import { verifyingToken } from "../../helpers";
    import history from "../../history";

    const SigninForm = ({ background, textColor, label, size, storeToken }) => {
      const [username, setUsername] = useState("");
      const [password, setPassword] = useState("");
      const [isAuthenticate, setAuthenticationStatus] = useState(undefined);
      const [loading, setLoading] = useState(false);
      const [realm, setRealm] = useState("test-realm-0");
      const [clientId, setClientId] = useState("test-realm-client");

      const submitSignInForm = async () => {
        try {
          setLoading(true);
          let body = new URLSearchParams({ username, password });
          const { data } = await axios.post(
            `${process.env.REACT_APP_SERVER_URL}/auth/${realm}/${clientId}`,
            body
          );
          setLoading(false);
          const isTokenVerified = verifyingToken(data, username);
          console.log("isTokenVerified", isTokenVerified);
          if (isTokenVerified) {
            setAuthenticationStatus(true);
            storeToken(data);
            history.push("/chat");
          } else {
            setAuthenticationStatus(false);
          }

          return data;
        } catch (error) {
          setLoading(false);
          setAuthenticationStatus(false);
          console.log(error);
        }
      };

      return (
        <div className={styles.SigninForm} data-testid="sign-in-form">
          <div className="signin-container">
            <h2>Great to have you here</h2>
            {/*Sub-component:-Email*/}

            <Form layout={"vertical"}>
              <Form.Item
                name={["user", "username"]}
                label="Enter username"
                hasFeedback
              >
                <Input
                  className={size}
                  value={username}
                  onChange={(e) => setUsername(e.target.value)}
                  placeholder="example@gmail.com"
                />
              </Form.Item>

              {/*Sub-component:-Password*/}

              <Form.Item
                name="password"
                label="Password"
                value={password}
                onChange={(e) => setPassword(e.target.value)}
                rules={[
                  {
                    type: "regexp",
                  },
                  ({ getFieldValue }) => ({
                    validator(_, value) {
                      if (
                        !value ||
                        getFieldValue("password").match(
                          new RegExp(
                            "^(?=.*[A-Za-z])(?=.*[@$!%*#?&])[A-Za-z0-9@$!%*#?&]{6,}$"
                          )
                        )
                      ) {
                        return Promise.resolve();
                      }
                      return Promise.reject(
                        new Error(
                          "Password should be atleast 6 characters long and should contain a special symbol!"
                        )
                      );
                    },
                  }),
                ]}
                hasFeedback
              >
                <Input.Password className={size} placeholder="Password" />
              </Form.Item>
              <div className="signin-btn">
                <Button
                  style={{ background: background, color: textColor }}
                  loading={!loading ? false : true}
                  onClick={() => submitSignInForm()}
                >
                  {label}
                  <span>
                    <FontAwesomeIcon icon={faArrowAltCircleRight} />
                  </span>
                </Button>
              </div>
              {isAuthenticate === false ? (
                <div className={styles.alert}>
                  <Alert
                    message="Error"
                    description="Couldn’t find a HCInbox account associated with this username.
                Please try again."
                    type="error"
                    showIcon
                  />
                </div>
              ) : null}
            </Form>
          </div>
        </div>
      );
    };

    SigninForm.propTypes = {
      background: PropTypes.string,
      textColor: PropTypes.string,
      size: PropTypes.string,
      storeToken: PropTypes.func,
    };

    SigninForm.defaultProps = {
      background: "rgba(9, 109, 217, 1)",
      textColor: "white",
      label: "Sign in",
      size: "medium",
      storeToken: (token) => {
        // console.log("token", token);
        sessionStorage.setItem("token", token);
      },
    };

    export default SigninForm;