Advertisement
Guest User

Untitled

a guest
Sep 11th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import { Button, FormGroup, FormControl, ControlLabel } from "react-bootstrap";
  3. import "./Login.css";
  4. //import { BrowserRouter as Router, Route } from "react-router-dom";
  5. //import { Link } from "react-router-dom";
  6. import axios from "axios";
  7. import Home from "./Home";
  8. import Header from "./Header";
  9. import { BrowserRouter as Router, Link, Redirect } from "react-router-dom";
  10. import { Route } from "react-router";
  11. import { createBrowserHistory } from "history";
  12. import logo from "./Logo";
  13.  
  14.  
  15.  
  16. //fetch('http:// localhost:52654/Service1.svc/Login', {
  17. // method: 'POST',
  18. // headers: {
  19. // 'Accept': 'application/json',
  20. // 'Content-Type': 'application/json',
  21. // },
  22. // body: JSON.stringify({
  23. // user: ' ' ,
  24. // password: ' ' ,
  25. // })
  26. //})
  27.  
  28. const API = "https://services.testnisite.com:8103/PublicWin/JoinVenture/Service1.svc/Login";
  29.  
  30. export default class Login extends Component {
  31. constructor(props) {
  32. super(props);
  33. // this.openHome = this.openHome.bind(this);
  34. //this.returnToLogin = this.returnToLogin.bind(this);
  35. this.state = {
  36. user: "",
  37. password: "",
  38. isLogged: false,
  39. redirect: '',
  40.  
  41.  
  42. };
  43. //this.switch2home = this.switch2home.bind(this);
  44. // activeComponent: <Home changeState={this.changeState.bind(this)} />
  45. /* this.fetchuser = this.fetchuser.bind(this);*/
  46. this.handleSubmit = this.handleSubmit.bind(this);
  47. // this.onChange = this.onChange.bind(this);
  48. this.handleChange = this.handleChange.bind(this);
  49. // this.onClick = this.onClick.bind(this);
  50. }
  51.  
  52.  
  53.  
  54. /*--*/
  55. // changeState = () => {
  56. // this.setState({ activeComponent: <Home /> })
  57.  
  58. /* fetchuser = () => {
  59. axios.get('ht tp://l ocalhost:52654/Service1.svc/Login/')
  60. .then((response) => {
  61. this.setState({ user: response.data, password: response.data })
  62. .cath((error) => {
  63.  
  64. console.log(error);
  65. });
  66. }
  67.  
  68. }*/
  69.  
  70.  
  71.  
  72. validateForm() {
  73. return this.state.user.length > 0 && this.state.password.length > 0;
  74. }
  75.  
  76. handleChange = event => {
  77. this.setState({
  78. [event.target.id]: event.target.value
  79. });
  80. console.log(event.target.id);
  81. };
  82.  
  83. redirect(path) {
  84. this.props.history.push(path);
  85. }
  86.  
  87. handleSubmit = event => {
  88. event.preventDefault();
  89. axios({
  90. method: "post",
  91. url: API,
  92. data: {
  93. user: this.state.user,
  94. password: this.state.password
  95. },
  96. withCredentials: false,
  97. responseType: "json",
  98. responseEncoding: "utf8"
  99. }).then(response => {
  100. console.log("login", response);
  101.  
  102. if (response.status == 200) {
  103. this.setState
  104. ({
  105. user: this.state.user,
  106. password: this.state.password
  107.  
  108. });
  109. // this.props.history.push("/Home");
  110. var res = JSON.parse(response.data.LoginResult);
  111.  
  112. if (res[0].isAllowed) {
  113. this.redirect("/Home");
  114. }
  115. else {
  116. this.redirect("/Error");
  117. }
  118.  
  119. console.log("Token: " + res[0].Token + " - isAllowed: " + res[0].isAllowed);
  120. localStorage.setItem("Token", res[0].Token)
  121.  
  122. console.log(localStorage.getItem("Token"));
  123. }
  124.  
  125.  
  126. }).catch((err) => {
  127. document.getElementById("user").value = "";
  128. document.getElementById("password").value = "";
  129. this.setState({ user: "", password: "" });
  130.  
  131. document.getElementById("user").focus();
  132. });
  133. };
  134.  
  135.  
  136.  
  137. render() {
  138.  
  139. /*if (this.state.Login === 'Login') {
  140. return < Route path="/" exact={true} component={Home} />
  141. }*/
  142.  
  143. //if (this.props.history.push === true) {
  144. // return<Redirect to="/" />
  145. // }
  146.  
  147.  
  148.  
  149. return (
  150.  
  151.  
  152. <div className="Login">
  153. <form onSubmit={this.handleSubmit.bind(this)}>
  154. <FormGroup controlId="user" bsSize="large">
  155. <ControlLabel>User</ControlLabel>
  156. <FormControl
  157. autoFocus
  158. type="user"
  159. value={this.state.user}
  160. onChange={this.handleChange.bind(this)}
  161. />
  162. </FormGroup>
  163. <FormGroup controlId="password" bsSize="large">
  164. <ControlLabel>Parola</ControlLabel>
  165. <FormControl
  166. value={this.state.password}
  167. onChange={this.handleChange.bind(this)}
  168. type="password"
  169. />
  170. </FormGroup>
  171. <Button
  172. block
  173. bsSize="large"
  174. disabled={!this.validateForm()}
  175. //onClick={this.handleClick.bind(this)}
  176. type="submit"
  177. >
  178. Conectare
  179. </Button>
  180.  
  181.  
  182. <div className="slica" >
  183. <img src="https://10pariuri.ro/wp-content/uploads/2018/02/Agentia-de-pariuri-online-Publicwin.png" height="300px" width="250px" />
  184.  
  185.  
  186. </div>
  187. </form>
  188.  
  189. </div>
  190. );
  191. }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement