authorization.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import React, {useState} from 'react';
  2. import {Link} from 'react-router-dom';
  3. import { actionAuthLogin } from '../store/authReducer';
  4. import { store } from '../store/store';
  5. function sendForm (url, data) {
  6. fetch(`http://player-api/api/${url}`, {
  7. method: 'POST',
  8. body: data
  9. }).then(res => res.json())
  10. .then(data => {
  11. if(data.token) {
  12. store.dispatch(actionAuthLogin(data.token, data.user));
  13. //console.log(data)
  14. return data
  15. } else {
  16. //console.log(data.login[0]);
  17. }
  18. })
  19. }
  20. export const LoginForm = () => {
  21. const [login, setLogin] = useState('');
  22. const [password, setPassword] = useState('');
  23. const postForm = (event) =>{
  24. event.preventDefault();
  25. const data = new FormData();
  26. data.append("login", login);
  27. data.append("password", password);
  28. sendForm('login', data);
  29. }
  30. return <>
  31. <form onSubmit={postForm} className="authorization container align-items-center justify-content-center vw-100 vh-100 d-flex">
  32. <div className="col-4">
  33. <h4 className="w-100 text-center">Login</h4>
  34. <hr/>
  35. <label className="form-label">Username</label><br/>
  36. <input type="text" id="username" className='input form-control mb-3' value={login} onChange={e => setLogin(e.target.value)}/>
  37. <label className="form-label">Password</label>
  38. <input type="password" id="password" className='form-control mb-3' value={password} onChange={e => setPassword(e.target.value)}/>
  39. <div className="d-flex justify-content-between">
  40. <Link to="/register" className="">Register</Link>
  41. <button type='submit' className="btn btn-outline-danger" onClick={() => console.log(login, password)}>Log in</button>
  42. </div>
  43. </div>
  44. </form>
  45. </>
  46. }
  47. export const RegisterForm = () => {
  48. const [login, setLogin] = useState('');
  49. const [password, setPassword] = useState('');
  50. const [name, setName] = useState('');
  51. const postForm = (event) =>{
  52. event.preventDefault();
  53. const data = new FormData();
  54. data.append("login", login);
  55. data.append("password", password);
  56. data.append("name", name);
  57. sendForm('register', data);
  58. }
  59. return (
  60. <form onSubmit={postForm} id="register_form">
  61. <div className="register container align-items-center justify-content-center vw-100 vh-100 d-flex">
  62. <div className="col-4">
  63. <h4 className="w-100 text-center">Sign Up</h4>
  64. <hr/>
  65. <label className="form-label">Login</label><br/>
  66. <input type="text" id="username" className="input form-control mb-3" placeholder="asya123"
  67. value={login} onChange={e => setLogin(e.target.value)}
  68. />
  69. <label className="form-label">Full Name</label><br/>
  70. <input type="text" id="name" className="input form-control mb-3" placeholder="My Chemical Romance"
  71. value={name} onChange={e => setName(e.target.value)}
  72. />
  73. <label className="form-label">Password</label>
  74. <input type="password" id="password" className="form-control mb-3" value={password} onChange={e => setPassword(e.target.value)} />
  75. <label className="form-label">Confirm password</label>
  76. <input type="password" id="confirm_password" className="form-control mb-3"/>
  77. <div className="d-flex justify-content-between align-items-center">
  78. <h6>Already a member? <Link to="/login" className="">Log in</Link></h6>
  79. <button type="submit" className="btn btn-outline-danger">Sign Up</button>
  80. </div>
  81. </div>
  82. </div>
  83. </form>
  84. )
  85. }