Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 657 | Rev 662 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
import React from "react";
2
import { useRef, useState, useEffect } from "react";
3
import { useForm } from "react-hook-form";
4
import styled from "styled-components";
5
import {axios} from "../../../../utils";
6
import FormErrorFeedback from "../../../../shared/form-error-feedback/FormErrorFeedback";
7
import Recaptcha from "react-recaptcha";
8
import Spinner from "../../../../shared/loading-spinner/Spinner";
655 steven 9
import cryptoJsAesJson from "../../../../utils/crypto-js/cryptojs-aes-format";
1 www 10
 
11
const StyledSpinnerContainer = styled.div`
12
  position: absolute;
13
  left: 0;
14
  top: 0;
15
  width: 100%;
16
  height: 100%;
17
  background: rgba(255, 255, 255, 0.4);
18
  display: flex;
19
  justify-content: center;
20
  align-items: center;
21
  z-index: 300;
22
`;
23
 
24
const Login = (props) => {
25
  // props destructuring
26
  const {
27
    captchaKey,
28
    addNotification,
29
    facebookOauth,
30
    twitterOauth,
31
    googleOauth,
32
    aes,
33
  } = props;
34
 
35
  const {
36
    register,
37
    handleSubmit,
38
    errors,
39
    setError,
40
    clearErrors,
41
    getValues,
42
    setValue,
43
  } = useForm();
44
 
45
  // Recaptcha
46
  const reCaptchaToken = useRef("");
47
  const [isVerified, setIsVerified] = useState(false);
48
  const reCaptchaInstance = useRef();
49
 
50
  // states
51
  const [rememberChecked, setRememberChecked] = useState(false);
52
  const [isLoading, setIsLoading] = useState(false);
53
 
54
  useEffect(() => {
55
    reCaptchaInstance.current.reset();
56
  }, []);
57
 
58
  const onSubmitHandler = async (data, event) => {
59
    // setIsLoading(true);
60
    const formData = new FormData();
61
    Object.entries(data).map(([key, value]) => {
657 steven 62
      console.log('>>: aes > test > eee ', cryptoJsAesJson.encrypt(value, aes), CryptoJSAesJson.encrypt(value, aes))
1 www 63
      if (key === "email" && value)
658 steven 64
        return formData.append(key, cryptoJsAesJson.encrypt(value, aes));
1 www 65
      if (key === "password" && value)
658 steven 66
        return formData.append(key, cryptoJsAesJson.encrypt(value, aes));
1 www 67
      if (value) formData.append(key, value);
68
    });
69
    formData.append("captcha", reCaptchaToken.current);
70
    await axios
71
      .post("/signin", formData)
72
      .then((response) => {
73
        const resData = response.data;
74
        if (resData.success) {
75
          window.location.replace(resData.data);
76
          event.target.reset();
77
        } else {
78
          setIsLoading(false);
79
          const resError = resData.data;
80
          if (resError.constructor.name === "Object") {
81
            Object.entries(resError).map(([key, value]) => {
82
              if (key in getValues()) {
83
                setError(key, {
84
                  type: "manual",
85
                  message: Array.isArray(value) ? value[0] : value,
86
                });
87
              }
88
            });
89
          } else {
90
            addNotification({
91
              style: "danger",
92
              msg: resError,
93
            });
94
          }
95
        }
96
      })
97
      .catch((error) => {
98
        setIsLoading(false);
99
      });
100
    reCaptchaInstance.current.reset();
101
    loginExpiredCallbackHandler();
102
    // setIsLoading(false);
103
    setValue("password", "");
104
  };
105
 
106
  const loginVerifyCallbackHandler = (response) => {
107
    reCaptchaToken.current = response;
108
    setIsVerified(true);
109
  };
110
 
111
  const loginExpiredCallbackHandler = () => {
112
    reCaptchaToken.current = "";
113
    setIsVerified(false);
114
  };
115
  const handleOnRecaptchaLoad = () => {
116
    reCaptchaToken.current = "";
117
  };
118
 
119
  const getRedirectUrl = async (endpoint) => {
120
    try {
121
      const res = await axios.get(endpoint)
122
      if(res.data && res.data.data){
123
        window.location.href = res.data.data
124
      }
125
    } catch (error) {
126
       ('>>: error > ', error)
127
    }
128
  }
129
  return (
130
    <React.Fragment>
131
      <h3>Entrar</h3>
132
      <form onSubmit={handleSubmit(onSubmitHandler)}>
133
        <div className="row">
134
          <div className="col-lg-12 no-pdd">
135
            <div className="inputContainer">
136
              <div className="sn-field">
137
                <input
138
                  type="email"
139
                  name="email"
140
                  ref={register({
141
                    required: "Este campo es requerido",
142
                  })}
143
                  placeholder="Correo electrónico"
144
                  maxLength="64"
145
                  // onChange={() => clearErrors("email")}
146
                />
147
                <i className="la la-envelope"></i>
148
              </div>
149
              {errors.email && (
150
                <FormErrorFeedback>{errors.email.message}</FormErrorFeedback>
151
              )}
152
            </div>
153
          </div>
154
          <div className="col-lg-12 no-pdd">
155
            <div className="inputContainer">
156
              <div className="sn-field">
157
                <input
158
                  type="password"
159
                  name="password"
160
                  // pattern="^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$^x%x*-]).{6,16}$"
161
                  ref={register({
162
                    required: "Este campo es requerido",
163
                  })}
164
                  placeholder="Clave"
165
                  maxLength="16"
166
                />
167
                <i className="la la-lock"></i>
168
              </div>
169
              {errors.password && (
170
                <FormErrorFeedback>{errors.password.message}</FormErrorFeedback>
171
              )}
172
            </div>
173
          </div>
174
 
175
          <div className="col-lg-12 no-pdd">
176
            <div className="inputContainer">
177
              <div className="checky-sec">
178
                <div className="fgt-sec">
179
                  <input
180
                    type="checkbox"
181
                    name="remember"
182
                    id="remember"
183
                    checked={rememberChecked}
184
                    ref={register}
185
                    value="1"
186
                    readOnly
187
                  />
188
                  <label
189
                    forhtml="remember"
190
                    onClick={() => setRememberChecked(!rememberChecked)}
191
                  >
192
                    <span></span>
193
                  </label>
194
                  <small onClick={() => setRememberChecked(!rememberChecked)}>
195
                    Recuerdame
196
                  </small>
197
                </div>
198
              </div>
199
            </div>
200
          </div>
201
 
202
          <div className="col-lg-12 no-pdd">
203
            <div className="sn-field border">
204
              <Recaptcha
205
                sitekey={captchaKey}
206
                verifyCallback={loginVerifyCallbackHandler}
207
                verifyCallbackName="loginVerifyCallbackHandler"
208
                expiredCallback={loginExpiredCallbackHandler}
209
                expiredCallbackName="loginExpiredCallbackHandler"
210
                ref={reCaptchaInstance}
211
                render="explicit"
212
                onloadCallback={handleOnRecaptchaLoad}
213
                hl="es"
214
              />
215
            </div>
216
          </div>
217
          <div className="col-lg-12 no-pdd"></div>
218
          <div className="col-lg-12 no-pdd">
219
            <button
220
              type="submit"
221
              value="submit"
222
              id="btn-submit"
223
              disabled={!isVerified}
224
            >
225
              Entrar
226
            </button>
227
          </div>
228
        </div>
229
      </form>
230
      {
231
        window.location.hostname.includes('dev') && (
232
          <div className="login-resources">
233
            <h4>Entrar usando su red social</h4>
234
            <ul>
235
              <li>
236
                <a onClick={() => getRedirectUrl(facebookOauth)}
237
                title=""
238
                className="cursor-pointer fb text-white">
239
                  <i className="fa fa-facebook"></i> FACEBOOK
240
                </a>
241
              </li>
242
              <li>
243
                <a
244
                  onClick={() => getRedirectUrl(twitterOauth)}
245
                  title=""
246
                  className="cursor-pointer tw btn-connect-social-media text-white"
247
                >
248
                  <i className="fa fa-twitter"></i> TWITTER
249
                </a>
250
              </li>
251
              <li
252
                className="bg-secondary"
253
              >
254
                <a
255
                  onClick={() => getRedirectUrl(googleOauth)}
256
                  title=""
257
                  className="cursor-pointer go btn-connect-social-media text-white"
258
                >
259
                  <i className="fa fa-google"></i> GOOGLE
260
                </a>
261
              </li>
262
            </ul>
263
          </div>
264
        )
265
      }
266
      {isLoading ? (
267
        <StyledSpinnerContainer>
268
          <Spinner />
269
        </StyledSpinnerContainer>
270
      ) : (
271
        ""
272
      )}
273
    </React.Fragment>
274
  );
275
};
276
 
277
export default Login;