I have an app deployed on Heroku. The problem is when I run it on Heroku, the value of auth token does not store in browser’s local storage (when I run it locally everything works just fine).
Here is the JS code that gets the token and stores it:
document.querySelector("#submit").onclick = function(){
var object = {
"email": document.getElementById("email").value,
"password": document.getElementById("password").value
};
sendRequest('POST', '/auth/sign-in', object)
.then(data => {localStorage.token = data.token;console.log(data.token)})
.catch(err => console.log(err))
}
function sendRequest(method, url, params = null) {
return new Promise ( (resolve, reject) => {
const xhr = new XMLHttpRequest()
xhr.open(method, url)
xhr.responseType = 'json'
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.onload = () => {
if(xhr.status >= 400) {
reject(xhr.response)
} else {
resolve(xhr.response)
}
}
xhr.onerror = () => {
reject(xhr.response)
}
xhr.send(JSON.stringify(params))
})
}
The console always shows “null” when I submit the form and this code runs.
On the server’s side the email and password are getting checked in database and JSON with the token is sent. My backend is on Golang, the token is sent like this:
func (h *Handler) signIn(c *gin.Context) {
...
c.JSON(http.StatusOK, map[string]interface{}{
"token": token,
})
}
I logged the value of token before it is sent from the server and it seems like the token is getting generated fine.
I would be grateful for any help!