Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import Navigation from './Navigation.js'
  3.  
  4. import qs from 'qs';
  5.  
  6. class Login extends Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. data: null,
  11. username: "",
  12. password: "",
  13. error: '',
  14. fetchPassword: '',
  15. exists: false,
  16.  
  17. };
  18. this.handlePassChange = this.handlePassChange.bind(this);
  19. this.handleUserChange = this.handleUserChange.bind(this);
  20. this.handleSubmit = this.handleSubmit.bind(this);
  21. this.dismissError = this.dismissError.bind(this);
  22. }
  23.  
  24.  
  25. getToken() {
  26. var obj;
  27. fetch('http://localhost:2018/WebApi/token', {
  28. method: 'POST',
  29. headers: {
  30. 'Content-Type': 'application/x-www-form-urlencoded'
  31. },
  32. body: qs.stringify({
  33. username: 'FEUP',
  34. password: 'qualquer1',
  35. company: 'FRUITSHOP',
  36. instance: 'Default',
  37. grant_type: 'password',
  38. Line: 'professional'
  39. })
  40. }).then(response => response.json())
  41. .then(function(data){
  42. obj = JSON.parse(JSON.stringify(data));
  43. })
  44. .then(() => {
  45. this.validateUser(obj.access_token);
  46. });
  47. console.log(this.state);
  48. console.log(obj);
  49. }
  50.  
  51. dismissError() {
  52. this.setState({ error: '' });
  53. }
  54.  
  55. handleSubmit(evt) {
  56. evt.preventDefault();
  57.  
  58. if (!this.state.username) {
  59. return this.setState({ error: 'Username is required' });
  60. }
  61.  
  62. if (!this.state.password) {
  63. return this.setState({ error: 'Password is required' });
  64. }
  65. //this.validateUser();
  66. this.getToken();
  67. return this.setState({ error: '' });
  68. }
  69.  
  70. handleUserChange(evt) {
  71. this.setState({
  72. username: evt.target.value,
  73. });
  74. };
  75.  
  76. handlePassChange(evt) {
  77. this.setState({
  78. password: evt.target.value,
  79. });
  80. }
  81.  
  82. validateUser(token){
  83. var baseURL= 'http://localhost:2018/WebApi/Base/Clientes/Existe/';
  84. var newURL= baseURL+this.state.username;
  85. console.log(newURL);
  86. fetch(newURL,{
  87. method: 'GET',
  88. headers: {
  89. 'Content-Type': 'application/x-www-form-urlencoded',
  90. 'cache-control': 'no-cache',
  91. 'Authorization': 'Bearer '+token
  92. },
  93. }).then(response => response.json())
  94. .then( exists => this.setState({exists}))
  95. .then(() => {
  96. console.log("EXISTE?="+this.state.exists)
  97. console.log(this.state);
  98. if(this.state.exists===true){
  99. console.log("user existe");
  100. this.validatePassword(token);
  101. }
  102. else if( this.state.exists===false){
  103. console.log('user:'+ this.state.username+ " nao existe");
  104. this.setState({ error: 'User nao existe' })
  105. }
  106. });
  107.  
  108.  
  109.  
  110.  
  111. }
  112.  
  113.  
  114. validatePassword(token){
  115. //Base/Clientes/DaValorAtributo/C001/CDU_CampoVar2
  116. var baseURL= 'http://localhost:2018/WebApi/Base/Clientes/DaValorAtributo/';
  117. var newURL= baseURL+this.state.username+'/CDU_CampoVar2';
  118.  
  119. console.log("URL:"+newURL);
  120. var obj;
  121. fetch(newURL,{
  122. method: 'GET',
  123. headers: {
  124. 'Authorization': 'Bearer '+token,
  125. 'Content-Type': 'application/json',
  126.  
  127. },
  128. }).then(response => response.json())
  129. .then(function(data){
  130. obj = JSON.parse(JSON.stringify(data));
  131. })
  132. .then(() => {
  133. console.log("obj:"+obj);
  134. if(obj===this.state.password){
  135. console.log("login successful");
  136. //store.set('loggedIn', true);
  137. //history.push('/users');
  138. }
  139. });
  140.  
  141.  
  142.  
  143. }
  144.  
  145. render() {
  146. return (
  147. <div>
  148. <Navigation />
  149. <div className="container mt-5">
  150. <div className="row">
  151. <div className="mx-auto">
  152. <div className="card card-signin my-5">
  153. <div className="card-body">
  154. <h5 className="card-title text-center">Sign In</h5>
  155. <form className="form-signin" onSubmit={this.handleSubmit}>
  156. {
  157.  
  158. this.state.error &&
  159. <div className="alert alert-warning alert-dismissible fade show" role="alert">
  160. <button type="button" className="close" onClick={this.dismissError} data-dismiss="alert" aria-label="Close">
  161. <span aria-hidden="true">&times;</span>
  162. </button>
  163. <strong> {this.state.error}</strong>
  164. </div>
  165.  
  166.  
  167. }
  168. <div className="form-label-group mb-3">
  169. <label>Username: </label>
  170. <input type="text" data-test="username" value={this.state.username} onChange={this.handleUserChange} />
  171. </div>
  172. <div className="form-label-group mb-3">
  173. <label>Password: </label>
  174. <input type="password" data-test="password" value={this.state.password} onChange={this.handlePassChange} />
  175. </div>
  176.  
  177. <button className="btn btn-primary btn-block text-uppercase" type="submit">Sign in</button>
  178.  
  179. </form>
  180. </div>
  181. </div>
  182. </div>
  183. </div>
  184. </div>
  185. </div>
  186. );
  187. }
  188. }
  189.  
  190. export default Login;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement