Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question In the below code I have written a simple user login page using React. Also, I have created my own backend using Express JS

Question

In the below code I have written a simple user login page using React. Also, I have created my own backend using Express JS + NodeJS and connected with MongoDB . From the frontend I am trying to authenticate user via http://localhost:5000/api/users. In the console I am getting this output after submitting the form index.js:28 POST http://localhost:5000/api/users 400 (Bad Request). Can someone correct the errors in this code? Thank you!

import React, { useState } from "react";

import {

Container,

Form,

FormButton,

FormContent,

FormH1,

FormInput,

FormLabel,

FormWrap,

Icon,

Text,

} from "./SigninElements";

const SignIn = () => {

const [email, setEmail] = useState("");

const [password, setPassword] = useState("");

const handleEmailChange = (e) => {

setEmail(e.target.value);

};

const handlePasswordChange = (e) => {

setPassword(e.target.value);

};

const handleSubmit = (e) => {

e.preventDefault();

fetch("http://localhost:5000/api/users", {

method: "POST",

body: JSON.stringify({

email: `"${email}"`,

password: `"${password}"`,

}),

headers: {

"Content-Type": "application/json",

},

})

.then((res) => {

if (res.status === 200) {

console.log("Login Success");

} else {

const error = new Error(res.error);

throw error;

}

})

.catch((err) => {

console.error(err);

alert("Error logging in please try again");

});

e.target.email.value = "";

e.target.password.value = "";

};

return (

to="/">Sushi Finder

action="POST" onSubmit={handleSubmit}>

Sign in to your account

htmlFor="for">Email

type="email"

required

onChange={handleEmailChange}

id="email"

/>

htmlFor="for">Password

type="password"

required

onChange={handlePasswordChange}

id="password"

/>

type="submit">Continue

Forgot Password

);

};

export default SignIn;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Reliability Engineering Designing And Operating Resilient Database Systems

Authors: Laine Campbell, Charity Majors

1st Edition

978-1491925942

More Books

Students also viewed these Databases questions

Question

It would have cost more to complain.

Answered: 1 week ago

Question

What is an interface? What keyword is used to define one?

Answered: 1 week ago

Question

=+C&B (especially taxation) laws, regulations, and practices?

Answered: 1 week ago