承诺后的重定向处理



我正在处理网站的身份验证部分,这是用户发送连接表单时调用的方法:

login() {
this.$store.dispatch('RETRIEVE_TOKEN', {
username: this.username,
password: this.password,
})
}

行动:

RETRIEVE_TOKEN(context, credentials) {
return new Promise((resolve, reject) => {
axios.post('someurl/auth', {
email: credentials.username,
password: credentials.password
})
.then(response => {
const token = response.data.key
localStorage.setItem('key', token)
context.commit('retrieveToken', token)
resolve(response)
this.$router.push('/backoffice')
})
.catch(error => {
console.log(error);
reject(error)
});
})
}

我的问题是,即使用户发送了错误的密码和用户邮件,也会调用this.$router.push('/backoffice')。我不明白为什么。有人能给我解释一下吗?

即使用户名和密码无效,服务器也会返回一个空密钥的200。我们可以添加一个条件来检查令牌是否可用

RETRIEVE_TOKEN(context, credentials) {
return axios.post('someurl/auth', {
email: credentials.username,
password: credentials.password
})
.then(response => {
console.log(response.data) // If it not works, please show what's logged here
const token = response.data.key
if (token) { // check if token is valid
localStorage.setItem('key', token)
context.commit('retrieveToken', token)
this.$router.push('/backoffice')
} else {
console.log('Token is not available')
}
})
.catch(error => {
console.log(error);
});
}

最新更新