Advertisement
shadiff

test123455087

Sep 12th, 2023
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. componentDidMount() {
  2. // Capture user-provided email and password from user input (e.g., form fields)
  3. const userEmail = this.state.userEmail; // Replace with actual state variable holding the user's email
  4. const userPassword = this.state.userPassword; // Replace with actual state variable holding the user's password
  5.  
  6. // Create an object to hold the user's credentials
  7. const credentials = {
  8. email: userEmail,
  9. password: userPassword,
  10. };
  11.  
  12. // Make a request to your login API with user-provided credentials
  13. fetch('yourLoginApiUrl', {
  14. method: 'POST',
  15. headers: {
  16. 'Content-Type': 'application/json',
  17. // Add any other headers required by your login API
  18. },
  19. body: JSON.stringify(credentials), // Send the user's credentials
  20. })
  21. .then((loginResponse) => loginResponse.json())
  22. .then((loginData) => {
  23. // Assuming your login API returns a Bearer token in 'loginData.token'
  24. const token = loginData.token;
  25.  
  26. // Now that you have the token, use it to fetch data from the protected API
  27. fetch('http://192.0.1.23:5000/scheme?id=208280', {
  28. headers: {
  29. Authorization: `Bearer ${token}`,
  30. // Add any other headers required by the protected API
  31. },
  32. })
  33. .then((response) => response.json())
  34. .then((data) => {
  35. this.props.setData(data);
  36. })
  37. .catch((error) => {
  38. console.error('Error fetching data from protected API:', error);
  39. });
  40. })
  41. .catch((loginError) => {
  42. console.error('Error during login:', loginError);
  43. });
  44. }
  45.  
  46. ---------------------
  47. fetch('http://192.0.1.23:5000/scheme?id=208280')
  48. .then((response) => response.json())
  49. .then((data) => {
  50. this.props.setData(data);
  51. })
  52. .catch((error) => {
  53. console.error('Error fetching data:', error);
  54. });
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement