signIn.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import React, { Component } from 'react';
  2. import Input from './input';
  3. import Button from './buttons/button';
  4. class SignIn extends Component {
  5. state = {
  6. form: {
  7. email: {
  8. id: 1,
  9. name: 'email',
  10. type: 'email',
  11. label: 'Email',
  12. validation: {
  13. requred: {
  14. cb: v => v.trim() === ""
  15. }
  16. },
  17. fail: false,
  18. touch: false,
  19. value: ''
  20. },
  21. password: {
  22. id: 2,
  23. name: 'password',
  24. type: 'password',
  25. label: 'Password',
  26. validation: {
  27. requred: {
  28. cb: v => v.trim() === ""
  29. },
  30. minL: {
  31. cb: v => v.trim().length < 6
  32. }
  33. },
  34. fail: false,
  35. touch: false,
  36. value: ''
  37. }
  38. },
  39. validForm: false
  40. };
  41. componentDidMount(){
  42. this.loadJS("https://apis.google.com/js/platform.js?onload=init");
  43. }
  44. loadJS = src => {
  45. const body = window.document.getElementsByTagName("body")[0];
  46. const ref = body.getElementsByTagName("script")[0];
  47. const script = window.document.createElement("script");
  48. script.src = src;
  49. script.async = true;
  50. script.defer = true;
  51. script.onload = () => {
  52. window.gapi.load("auth2", () => {
  53. console.log("success");
  54. });
  55. };
  56. ref.parentNode.insertBefore(script, ref);
  57. };
  58. googleAuth = () => {
  59. const ga = window.gapi.auth2.init({
  60. client_id: '1018884941403-cd6ilegbhitova86k4i7k89eqldpatfj.apps.googleusercontent.com'
  61. });
  62. ga.signIn().then(info => {
  63. const { Zi } = info;
  64. fetch("http://subdomain.entony.fs.a-level.com.ua/api/auth/google", {
  65. method: "POST",
  66. headers: {
  67. "Content-Type": "application/json"
  68. },
  69. body: JSON.stringify({ access_token: Zi.access_token })
  70. })
  71. .then(res => res.json())
  72. .then(res => console.log("res", res))
  73. .catch(err => console.log(err.response));
  74. });
  75. };
  76. instaAuth = () => {
  77. }
  78. validator = (rules,value) => {
  79. const { required, minL } = rules;
  80. let valid = true;
  81. if(required){
  82. valid = value.trim() === '' && valid;
  83. }
  84. if(minL){
  85. valid = value.trim().length < minL && valid;
  86. }
  87. return valid
  88. };
  89. submit = e => {
  90. e.preventDefault();
  91. const values = Object.keys(this.state.form).reduce((prev,elem) => {
  92. return {...prev, [elem]: this.state.form[elem].value };
  93. }, {});
  94. this.props.submitHandler(values);
  95. };
  96. onChangeHandler = e => {
  97. const { name, value } = e.target;
  98. this.setState(prevState => {
  99. const values = Object.keys(prevState.form).reduce((prev, elem) => {
  100. if (elem === name) return prev.concat(value);
  101. return prev.concat(prevState.form[elem].value);
  102. },[]);
  103. return {
  104. ...prevState,
  105. form: {
  106. ...prevState.form,
  107. [name]: {
  108. ...prevState.form[name],
  109. value,
  110. touch: true,
  111. fail: this.validator(prevState.form[name].validation, value)
  112. }
  113. },
  114. validForm: values.some(value => value)
  115. };
  116. });
  117. };
  118. render() {
  119. const { form, validForm } = this.state;
  120. const { error } = this.props;
  121. return (
  122. <div className="auth__auth-box">
  123. <form onSubmit={this.submit} className="auth__auth-form">
  124. {Object.keys(form).map(input_name => {
  125. return (
  126. <Input
  127. key={form[input_name].id}
  128. id={form[input_name].id}
  129. value={form[input_name].value}
  130. name={form[input_name].name}
  131. type={form[input_name].type}
  132. fail={form[input_name].fail}
  133. touch={form[input_name].touch}
  134. label={form[input_name].label}
  135. onChange={this.onChangeHandler}
  136. />
  137. );
  138. })}
  139. {error && <p className='auth__error-auth-text'>{error}</p>}
  140. <div className="auth__control-box">
  141. <Button className='auth__submit-btn'
  142. disabled={!validForm}
  143. type='submit'
  144. text='Sign In'/>
  145. </div>
  146. </form>
  147. <div className="auth__google-auth">
  148. <Button
  149. type="button"
  150. text='Sign in with Google'
  151. onClick={this.googleAuth}/>
  152. </div>
  153. </div>
  154. );
  155. }
  156. }
  157. export default SignIn;