Reputation: 63
I am trying to authenticate a user using an API. the login API is working fine. If a person is not registered it prints the message to register first. But the register function is not working. I am getting response code 400 on hitting the register API.
It shows the following message:
Missing parameters in request. Send user_name, email, password
I am not able to understand where am i going wrong. Ideally it should hit the API and create a new user and then should redirect to next page.
here is my code for sign up function
$(".form-item.sign-up .btn").click(
async () => {
get_email = document.getElementById("email_box2").value;
get_password = document.getElementById("password2").value;
get_username = document.getElementById("username").value;
console.log(get_email, get_password, get_username);
const response = await fetch('http://127.0.0.1:81/traveliobot/api/v1/auth/register', {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'email': get_email,
'password': get_password,
'username': get_username
}) // string or object
});
const myJson = await response.json(); //extract JSON from the http response
response_code = myJson.responseCode
console.log(response_code)
if (response_code == '0') {
user_id = myJson.user_id;
console.log(user_id)
linkLocation = 'chat_index.html';
window.sessionStorage.setItem("user_id", user_id);
$("body").fadeOut(1500, redirect_page)
} else {
console.log(myJson.responseDesc)
}
}
);
function redirect_page() {
window.location = linkLocation;
widn
}
login function:
$(".form-item.log-in .btn").click(
async () => {
get_email = document.getElementById("email_box1").value;
get_password = document.getElementById("password1").value;
const response = await fetch('http://127.0.0.1:81/traveliobot/api/v1/auth/login', {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'email': get_email,
'password': get_password
}) // string or object
});
const myJson = await response.json(); //extract JSON from the http response
response_code = myJson.responseCode
console.log(response_code)
if (response_code == '0') {
user_id = myJson.user_id;
console.log(user_id)
linkLocation = 'chatbot_index.html';
window.sessionStorage.setItem("user_id", user_id);
$("body").fadeOut(1500, redirect_page)
} else {
console.log(myJson.responseDesc)
}
}
);
function redirect_page() {
window.location = linkLocation;
widn
}
html file:
<div class="container-form">
<div class="form-item log-in">
<div class="table">
<div class="table-cell">
<input name="Username" placeholder="Email" type="text" id="email_box1" /><input name="Password"
placeholder="Password" type="password" id="password1" />
<div class="btn">
Log in
</div>
</div>
</div>
</div>
<div class="form-item sign-up">
<div class="table">
<div class="table-cell">
<input name="email" placeholder="Email" type="text" id="email_box2" />
<input name="Username" placeholder="Username"type="text" id="username" />
<input name="Password" placeholder="Password" type="Password" id="password2" />
<div class="btn">
Sign up
</div>
</div>
</div>
</div>
</div>
Any help would be appreciated. Thanks in advance.
Upvotes: 0
Views: 394
Reputation: 1881
You have username
instead of user_name
in the JSON payload of the register API call.
Upvotes: 1