Guest User

Untitled

a guest
Apr 8th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import axios from 'axios';
  3.  
  4. class Login extends Component {
  5. constructor(){
  6. super();
  7. this.state = {
  8. username: '',
  9. password: '',
  10. };
  11. }
  12.  
  13. navigatetopolls(token){
  14. try {
  15. const res = fetch('http://localhost:8000/api/polls/1',{
  16. headers: new Headers({
  17. 'Authorization' : 'Token'+ token
  18.  
  19. })
  20. });
  21. const polls = res.json();
  22. console.log(polls)
  23. this.State= { polls }
  24. } catch (e) {
  25. console.log(e);
  26. }
  27. }
  28.  
  29. onChange = (e) => {
  30. // Because we named the inputs to match their corresponding values in state, it's
  31. // super easy to update the state
  32. const state = this.state
  33. state[e.target.name] = e.target.value;
  34. this.setState(state);
  35. }
  36.  
  37. onSubmit = (e) => {
  38. e.preventDefault();
  39. // get our form data out of state
  40. const { username, password } = this.state;
  41.  
  42. axios.post('http://localhost:8000/api/login/', {
  43. username: username,
  44. password: password,
  45. })
  46. .then((response) => {
  47. console.log(response);
  48. let token =response.data['token'];
  49. localStorage.setItem("token", token)
  50. //this.navigatetopolls(token)
  51.  
  52. })
  53. .catch(function (error) {
  54. console.log(error);
  55. });
  56. }
  57.  
  58. render() {
  59. const { username, password } = this.state;
  60.  
  61. return(
  62. <form onSubmit={this.onSubmit}>
  63. <label htmlFor="username">Enter username</label>
  64. <input name="username" type="text" value={username} onChange={this.onChange}/>
  65.  
  66. <label htmlFor="password">Enter your password</label>
  67. <input name="password" type="password" value={password} onChange={this.onChange}/>
  68.  
  69. <button type="submit">Submit!</button>
  70. </form>
  71. );
  72. }
Add Comment
Please, Sign In to add comment