Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1.  
  2. authenticate() {
  3. fetch(this.state.authenticationURL, {
  4. headers: {
  5. "Content-Type": "application/json"
  6. },
  7. credentials: "include",
  8. method: "POST",
  9. body: JSON.stringify({
  10. username: this.state.username,
  11. password: this.state.password
  12. })
  13. })
  14. .then(response => {
  15. if (!response.ok) {
  16. response.json().then(error => {
  17. this.setState({
  18. showAlert: true,
  19. alertType: "danger",
  20. alertMessage: error.message
  21. });
  22. });
  23. Cookies.remove("userid");
  24. Cookies.remove("firstname");
  25. Cookies.remove("token");
  26. }
  27. return response.json();
  28. })
  29. .then(data => {
  30. this.setState({
  31. showAlert: true,
  32. alertType: "success",
  33. alertMessage: "Success!!"
  34. });
  35. Cookies.set("userid", data.userid);
  36. Cookies.set("firstname", data.firstname);
  37. Cookies.set("Token", data.token);
  38. window.location = this.state.redirectURL;
  39. })
  40. .catch(error => {
  41. console.error("Error cannot communicate with the API Server.");
  42. });
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement