My goal is to create a login interface using Plain Javascript. I have obtained a Token from the backend and now need assistance in utilizing this Token for the login process and storing it in LocalStorage.
Although I have successfully made the API call, I am encountering a 502 (Bad Gateway) error. It seems like the issue might be related to not properly setting the token.
function postData() {
var result = fetch('https://example.api.com/login', {
method: "POST",
mode: "cors",
cache: "no-cache",
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Access-Control-Allow-Origin': '*',
'Accept': 'application/json'
},
redirect: "follow",
referrer: "no-referrer",
body: JSON.stringify({
isArray: false,
data: {
email: document.getElementById("email").value,
password: document.getElementById("passwordNew").value
}
})
}).then(response => response.json());
console.log("result :" + result);
return result;
}
This code snippet shows the API call being made, which returns the token as part of the response.
The received response includes the following details:
"data": {
"token": "sdfsdgsfgsgsgssb497e7764f4df8cb504a122cc18b2eed8",
"startTime": 1558417495078,
"endTime": 1558503895078
}
Upon successful utilization of the token provided by the backend, I expect to achieve a successful login by entering my email and password.