``i am working on project and i have to add user in react js with api endpoints. Still the code not working. when i click on add user it add user but when i login it doesnt work. the user with api tsted on postman and works but not working on code.
import React, { useState } from "react";
const AddUser = ({ setTokenValue }) => {
const [isSubmitting, setIsSubmitting] = useState(false);
const [errorMessage, setErrorMessage] = useState("");
const [name, setName] = useState("");
const [email, setEmail] = useState("");
const [phone, setPhone] = useState("");
const [password, setPassword] = useState("");
const handleSubmit = async (event) => {
event.preventDefault();
setIsSubmitting(true);
try {
const response = await fetch("https://api.store.ellcart.com/users", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": Bearer <eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2FwaS5zdG9yZS5lbGxjYXJ0LmNvbS91c2Vycy9sb2dpbiIsImlhdCI6MTY4NTYzNDY1MCwibmJmIjoxNjg1NjM0NjUwLCJqdGkiOiJBZ2doWFpiT1VxeGF4QTBvIiwic3ViIjoiMjA0IiwicHJ2IjoiMjNiZDVjODk0OWY2MDBhZGIzOWU3MDFjNDAwODcyZGI3YTU5NzZmNyJ9.28tizEstlORenFGcDin-3RSFgtHzJpnoA2UYI7A2sJ0>
,
},
body: JSON.stringify({
name: name,
email: email,
phone: phone,
password: password,
}),
});
if (!response.ok) {
throw new Error("Failed to add user");
}
const data = await response.json();
if (data && data.authorisation && data.authorisation.access_token) {
const token = data.authorisation.access_token;
setTokenValue(token);
setName("");
setEmail("");
setPhone("");
setPassword("");
setErrorMessage("");
}
} catch (error) {
setErrorMessage(error.message);
console.error("Failed to add user:", error);
} finally {
setIsSubmitting(false);
}
};
return (
{errorMessage &&
{errorMessage}
}Name:
setName(e.target.value)} />
Email:
setEmail(e.target.value)} />
Phone:
setPhone(e.target.value)} />
Password:
setPassword(e.target.value)} />
{isSubmitting ? "Submitting..." : "Add User"}
);
};
export default AddUser;`
Top comments (0)