Advertisement
Guest User

Untitled

a guest
Sep 27th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. export function loginUser(creds) {
  2.  
  3. const data = querystring.stringify({_username: creds.username, _password: creds.password});
  4.  
  5. let config = {
  6. method: 'POST',
  7. headers: { 'Content-Type':'application/x-www-form-urlencoded' },
  8. body: data
  9. };
  10.  
  11. return dispatch => {
  12. // We dispatch requestLogin to kickoff the call to the API
  13. dispatch(requestLogin(creds));
  14.  
  15. return fetch(BASE_URL+'login_check', config)
  16. .then(response =>
  17. response.json().then(user => ({ user, response }))
  18. ).then(({ user, response }) => {
  19. if (!response.ok) {
  20. // If there was a problem, we want to
  21. // dispatch the error condition
  22. dispatch(loginError(user.message));
  23. return Promise.reject(user)
  24. } else {
  25.  
  26. localStorage.setItem('idToken', user.token);
  27. let token = localStorage.getItem('idToken')
  28. console.log(token);
  29. // if I remove this log, my token is returned as null during post.
  30. dispatch(receiveLogin(user));
  31. }
  32. }).catch(err => console.log("Error: ", err))
  33. }
  34. }
  35.  
  36. import axios from 'axios';
  37. import {BASE_URL} from './middleware/api';
  38. import {reset} from 'redux-form';
  39.  
  40. let token = localStorage.getItem('idToken');
  41. const AuthStr = 'Bearer '.concat(token);
  42.  
  43. let headers ={
  44. headers: { 'Content-Type':'application/json','Authorization' : AuthStr }
  45. };
  46.  
  47. export default (async function showResults(values, dispatch) {
  48. console.log(AuthStr);
  49. axios.post(BASE_URL + 'human/new', values, headers)
  50. .then(function (response) {
  51. console.log(response);
  52. alert("Your submit was successful");
  53. //dispatch(reset('wizard'));
  54. }).catch(function (error) {
  55. console.log(error.response);
  56. alert(error.response.statusText);
  57. });
  58. });
  59.  
  60. getHouses = (e) => {
  61. let token = localStorage.getItem('idToken') || null;
  62. const AuthStr = 'Bearer '.concat(token);
  63. axios.get(BASE_URL + 'household/list', { headers: { Authorization: AuthStr } }).then((response) =>
  64. {
  65. let myData = response.data;
  66.  
  67. let list = [];
  68. let key =[];
  69. for (let i = 0; i < myData._embedded.length; i++) {
  70. let embedded = myData._embedded[i];
  71. list.push(embedded.friendlyName);
  72. key.push(embedded.id);
  73. }
  74.  
  75. this.setState({data: list, key: key});
  76.  
  77. })
  78. .catch((error) => {
  79. console.log('error' + error);
  80. });
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement