Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- componentDidMount() {
- // Capture user-provided email and password from user input (e.g., form fields)
- const userEmail = this.state.userEmail; // Replace with actual state variable holding the user's email
- const userPassword = this.state.userPassword; // Replace with actual state variable holding the user's password
- // Create an object to hold the user's credentials
- const credentials = {
- email: userEmail,
- password: userPassword,
- };
- // Make a request to your login API with user-provided credentials
- fetch('yourLoginApiUrl', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- // Add any other headers required by your login API
- },
- body: JSON.stringify(credentials), // Send the user's credentials
- })
- .then((loginResponse) => loginResponse.json())
- .then((loginData) => {
- // Assuming your login API returns a Bearer token in 'loginData.token'
- const token = loginData.token;
- // Now that you have the token, use it to fetch data from the protected API
- fetch('http://192.0.1.23:5000/scheme?id=208280', {
- headers: {
- Authorization: `Bearer ${token}`,
- // Add any other headers required by the protected API
- },
- })
- .then((response) => response.json())
- .then((data) => {
- this.props.setData(data);
- })
- .catch((error) => {
- console.error('Error fetching data from protected API:', error);
- });
- })
- .catch((loginError) => {
- console.error('Error during login:', loginError);
- });
- }
- ---------------------
- fetch('http://192.0.1.23:5000/scheme?id=208280')
- .then((response) => response.json())
- .then((data) => {
- this.props.setData(data);
- })
- .catch((error) => {
- console.error('Error fetching data:', error);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement