loginPage.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import React, {useState} from 'react';
  2. import {Link} from 'react-router-dom';
  3. import Form from 'react-bootstrap/Form';
  4. import { actionAuthLogin } from '../store/authReducer';
  5. import { store } from '../store/store';
  6. import {history} from '../App';
  7. import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
  8. import {faEyeSlash, faEye} from "@fortawesome/free-solid-svg-icons";
  9. import { Button, InputGroup } from 'react-bootstrap';
  10. export async function SendForm (url, data) {
  11. let error = await fetch(`http://player-api/api/${url}`, {
  12. method: 'POST',
  13. body: data
  14. }).then(res => res.json())
  15. .then((data) => {
  16. if(data.token) {
  17. history.push('/user');
  18. store.dispatch(actionAuthLogin(data.token, data.user));
  19. return data
  20. } else {
  21. return data.message;
  22. }
  23. })
  24. console.log(error);
  25. return error;
  26. }
  27. export const LoginForm = () => {
  28. const [login, setLogin] = useState('');
  29. const [password, setPassword] = useState('');
  30. const [textModal, setTextModal] = useState('');
  31. const [showPass, setShowPass] = useState(false);
  32. const postForm = async(event) =>{
  33. event.preventDefault();
  34. const data = new FormData();
  35. data.append("login", login);
  36. data.append("password", password);
  37. setTextModal(( typeof(await SendForm('login', data))==='string')? (await SendForm('login', data)) : '');
  38. }
  39. return <>
  40. <Form onSubmit={postForm} className="authorization container align-items-center justify-content-center vw-100 vh-100 d-flex">
  41. <div className="col-4">
  42. <h4 className="w-100 text-center">Login</h4>
  43. <hr/>
  44. <Form.Group>
  45. <Form.Label className="form-label">*Username</Form.Label>
  46. <Form.Control type="text" id="username" className='input form-control mb-3'
  47. value={login} onChange={e => setLogin(e.target.value)} />
  48. </Form.Group>
  49. <Form.Group>
  50. <Form.Label className="form-label">*Password</Form.Label>
  51. <InputGroup>
  52. <Form.Control type={showPass ? "text" : "password"} id="password" className='form-control mb-3'
  53. value={password} onChange={e => setPassword(e.target.value)} />
  54. <InputGroup.Text className='mb-3'>
  55. <FontAwesomeIcon icon={showPass ? faEye : faEyeSlash} onClick={() => showPass? setShowPass(false) : setShowPass(true)}/>
  56. </InputGroup.Text>
  57. </InputGroup>
  58. </Form.Group>
  59. <p className='text-danger'>{textModal ? ('*' + textModal) : ''}</p>
  60. <div className="d-flex justify-content-between">
  61. <Link to="/register" className="">Register</Link>
  62. <Button type='submit' variant="outline-danger" disabled={ password.length < 8 || login.length < 5} onClick={() => console.log(textModal)}>Log in</Button>
  63. </div>
  64. </div>
  65. </Form>
  66. </>
  67. }