So as you can see, I'm using a loginform that validates user and password online, generates a token and is supossed to once the token exists redirect the page to pacientes.html
. the problem starts in renderApp
. It does execute the window.location.href
but to redirect Ihave to manually refresh the page. am I doing something wrong? pls help
let ruta = 'login'
const renderApp = () =>{
const token = localStorage.getItem('token')
if (token) {
window.location.href = "pacientes.html"
}
}
window.onload= () => {
renderApp()
const loginForm= document.getElementById('login')
loginForm.onsubmit = (e) =>{
e.preventDefault()
e.stopPropagation()
const cedula = document.getElementById('cedula').value
const password = document.getElementById('password').value
fetch('https://janfa.gharsnull.now.sh/api/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({cedula, password})
}).then( x => x.json())
.then( respuesta => {
localStorage.setItem('token' , respuesta.token)
ruta = 'users'
})
}
}
If you want to redirect when you get the response from the HTTP request, then put the code to redirect in the
then
callback.i.e. replace
ruta = 'users'
withlocation.href = "pacientes.html"
.如果将其放在render函数中,它将仅在组件渲染时执行!