I've been following the documentation for user authentication using my login form, as outlined in this link. Even though Use Case 4 is supposed to authenticate a user, I keep encountering an error indicating that my user is not authenticated, which is causing some confusion.
When I execute the authenticateUser
function, I am able to obtain a token and successfully redirect to another page. However, upon calling getUserAttributes
on that page, I receive an error stating that the user is not logged in.
In the login.vue file:
const userPool = new CognitoUserPool(cognitoConfig)
const authenticationData = {
Username: this.username,
Password: this.password
}
const authenticationDetails = new AuthenticationDetails(authenticationData)
const userData = {
Username: this.username,
Pool: userPool
}
const user = new CognitoUser(userData)
const self = this
user.authenticateUser(authenticationDetails, {
onSuccess: result => {
const token = result.getAccessToken().getJwtToken()
console.log('token = ', token)
self.$store.commit('user/SET_USERNAME', self.username)
self.$router.push('/dashboard')
},
onFailure: err => {
console.log('err = ', err)
}
})
And in the dashboard.vue file:
const userPool = new CognitoUserPool(cognitoConfig)
const userData = {
Username: this.user.username,
Pool: userPool
}
console.log('userData = ', userData)
const user = new CognitoUser(userData)
user.getUserAttributes((err, result) => {
if (err) {
console.log('getUserAttributes err = ', err)
alert(err.message || JSON.stringify(err))
return
}
for (let i = 0; i < result.length; i++) {
console.log('attribute ' + result[i].getName() + ' has value ' + result[i].getValue())
}
})