signIn.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import React, { useState } from "react";
  2. import { Link } from "react-router-dom";
  3. import { connect } from "react-redux";
  4. import {
  5. UserOutlined,
  6. LockOutlined,
  7. EyeOutlined,
  8. EyeInvisibleOutlined,
  9. } from "@ant-design/icons";
  10. import jwtDecode from "jwt-decode";
  11. import { actionLogin } from "./actionLogin/actionLogin";
  12. import { actionChatGroup } from "../myProfile/chatGroups/actionChatGroup/actionChatGroup";
  13. import "./signIn.css";
  14. const loginEye = () => {
  15. const password = document.getElementById("password");
  16. const notSee = document.querySelector(".not-see");
  17. const see = document.querySelector(".see");
  18. if (password.type === "password") {
  19. password.type = "text";
  20. see.style.display = "inline-block";
  21. notSee.style.display = "none";
  22. } else {
  23. password.type = "password";
  24. notSee.style.display = "inline-block";
  25. see.style.display = "none";
  26. }
  27. };
  28. const LoginForm = ({ onLogin, backError, state }) => {
  29. const [login, setLogin] = useState("");
  30. const [password, setPassword] = useState("");
  31. const way = (obj, resolverName, a) => {
  32. const stateObj = state[obj];
  33. for (var keys in stateObj) {
  34. for (var keyData in stateObj[keys]) {
  35. if (keyData === "data") {
  36. const allObj = stateObj[keys][keyData][resolverName];
  37. if (allObj) {
  38. var x = jwtDecode(allObj);
  39. localStorage.setItem("id", x.sub.id);
  40. localStorage.setItem("email", x.sub.email);
  41. localStorage.setItem("login", x.sub.login);
  42. } else {
  43. return (
  44. <div className="error">
  45. <p>Неверный логин или пароль, попробуйте заново.</p>
  46. </div>
  47. );
  48. }
  49. }
  50. }
  51. }
  52. };
  53. // console.log(localStorage.getItem("id"));
  54. const buttonEnter = (e) => {
  55. if (e.key === "Enter") onLogin(login, password);
  56. };
  57. // const handleClick = () => history.push("/my_profile");
  58. return (
  59. <div className="login">
  60. <div className="login-form" onKeyPress={buttonEnter}>
  61. <div className="login-title">
  62. <h2>Войти в аккаунт</h2>
  63. <p>Пожалуйста, войдите в свой аккаунт</p>
  64. </div>
  65. <div className="login-icon icon">
  66. <UserOutlined />
  67. <input
  68. id="login"
  69. value={login}
  70. placeholder="Логин"
  71. type="text"
  72. onChange={(e) => setLogin(e.target.value)}
  73. />
  74. </div>
  75. <div className="password-icon icon">
  76. <LockOutlined />
  77. <input
  78. id="password"
  79. value={password}
  80. placeholder="Пароль"
  81. type="password"
  82. onChange={(e) => setPassword(e.target.value)}
  83. />
  84. <div className="not-see" onClick={loginEye}>
  85. <EyeInvisibleOutlined />
  86. </div>
  87. <div className="see" onClick={loginEye}>
  88. <EyeOutlined />
  89. </div>
  90. </div>
  91. {way("login", "getLogin")}
  92. <button
  93. className="login-button"
  94. onClick={() => onLogin(login, password)}
  95. disabled={!login || !password}
  96. >
  97. Войти
  98. </button>
  99. <div className="register-put-password">
  100. <Link to="/sign_up">Зарегестрироваться</Link>
  101. <Link to="/put_password">Забыли пароль?</Link>
  102. </div>
  103. </div>
  104. </div>
  105. );
  106. };
  107. // class LoginForm extends Component {
  108. // constructor(props) {
  109. // super(props);
  110. // this.state = { login: "", password: "" };
  111. // // console.log(this.props);
  112. // }
  113. // way (obj, resolverName, a) {
  114. // const stateObj = this.props.state[obj];
  115. // for (var keys in stateObj) {
  116. // for (var keyData in stateObj[keys]) {
  117. // if (keyData === "data") {
  118. // const allObj = stateObj[keys][keyData][resolverName];
  119. // if (allObj) {
  120. // var x = jwtDecode(allObj);
  121. // // return x.sub.email;
  122. // // console.log(x);
  123. // } else {
  124. // return (
  125. // <div className="error">
  126. // <p>Неверный логин или пароль, попробуйте заново.</p>
  127. // </div>
  128. // );
  129. // }
  130. // }
  131. // }
  132. // }
  133. // };
  134. // way1 (obj, resolverName, a) {
  135. // const stateObj = this.props.state[obj];
  136. // for (var keys in stateObj) {
  137. // for (var keyData in stateObj[keys]) {
  138. // if (keyData === "data") {
  139. // const allObj = stateObj[keys][keyData][resolverName];
  140. // if (allObj) {
  141. // var x = jwtDecode(allObj);
  142. // return x.sub.email;
  143. // }
  144. // }
  145. // }
  146. // }
  147. // };
  148. // componentDidMount() {
  149. // this.props.chatGroup("2");
  150. // // this.props.message();
  151. // }
  152. // // buttonEnter (e) {
  153. // // if (e.key === "Enter") this.props.onLogin(login, password);
  154. // // };
  155. // // const handleClick = () => history.push("/my_profile");
  156. // render(){
  157. // return (
  158. // <div className="login">
  159. // <div className="login-form"
  160. // // onKeyPress={buttonEnter}
  161. // >
  162. // <div className="login-title">
  163. // <h2>Войти в аккаунт</h2>
  164. // <p>Пожалуйста, войдите в свой аккаунт</p>
  165. // </div>
  166. // <div className="login-icon icon">
  167. // <UserOutlined />
  168. // <input
  169. // id="login"
  170. // value={this.state.login}
  171. // placeholder="Логин"
  172. // type="text"
  173. // onChange={(e) => this.setState({ login: e.target.value })}
  174. // />
  175. // </div>
  176. // <div className="password-icon icon">
  177. // <LockOutlined />
  178. // <input
  179. // id="password"
  180. // value={this.state.password}
  181. // placeholder="Пароль"
  182. // type="password"
  183. // onChange={(e) => this.setState({ password: e.target.value })}
  184. // />
  185. // <div className="not-see" onClick={loginEye}>
  186. // <EyeInvisibleOutlined />
  187. // </div>
  188. // <div className="see" onClick={loginEye}>
  189. // <EyeOutlined />
  190. // </div>
  191. // </div>
  192. // {this.way("login", "getLogin")}
  193. // <button
  194. // className="login-button"
  195. // onClick={() => this.props.onLogin(this.state.login, this.state.password)}
  196. // disabled={!this.state.login || !this.state.password}
  197. // >
  198. // Войти
  199. // </button>
  200. // <div className="register-put-password">
  201. // <Link to="/sign_up">Зарегестрироваться</Link>
  202. // <Link to="/put_password">Забыли пароль?</Link>
  203. // </div>
  204. // </div>
  205. // </div>
  206. // )}
  207. // };
  208. const Login = (props) => <ConnectedLoginForm />;
  209. const ConnectedLoginForm = connect(
  210. (state) => {
  211. return { state };
  212. },
  213. { onLogin: actionLogin, chatGroup: actionChatGroup }
  214. )(LoginForm);
  215. export default Login;