Troubleshooting Issues with AWS Cognito Authentication

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())
  }
})

Answer №1

Ensure that in dashboard.vue, a new user instance is created which is not authenticated. Make sure to call getUserAttributes immediately after authenticateUser.

When authenticateUser is called and successfully executed, certain properties like token, signature are set on the user instance. Subsequent methods, such as getUserAttributes, rely on this information being present.

The issue with your code in dashboard.vue is that when you invoke the method, the user instance is not yet authenticated, leading to the error message displayed.

Remember to authenticate ALL user INSTANCEs to avoid any errors.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

using Javascript to eliminate necessary tags from concealed tabs

My goal is to dynamically remove the required tag from fields in hidden tabs when a tab is clicked using JavaScript. The presence of the required tag on unused fields causes issues with form submission, preventing data insertion into the database. Here&apo ...

The function window.open has been disabled on codepen.io site

My dilemma involves a button designed to open a random Wikipedia page. While the code works perfectly in the CodePen editor, I encounter issues when opening it in full-page view. The problem arises when the log displays 'window.open is disabled'. ...

Concerning geoext

I am currently working on creating a framework for overlaying layers on the web. I am using geoserver to publish the layers and the geoext tree.js example to display all the layers in a tree-like panel. However, I'm encountering an issue with zooming ...

What is the method for printing background images/styles without adjusting the browser's page setup?

Is there a method to maintain row striping in the printout like it appears on screen? <div id="test" style="background:#000000; color:#FFFFFF">Black Stripe</div> I would like to create a page with consistent row stripe formatting for printing ...

Could you provide insight into the reason behind debounce being used for this specific binding?

function debounce(fn, delay) { var timer return function () { var context = this var args = arguments clearTimeout(timer) timer = setTimeout(function () { fn.apply(context, args) }, delay) ...

Issue with React: Children's state is altering the state of the parent component

Question : Why is it possible for a child component to change the state of its parent when each component has its own state? Below is the complete code for my app: import React, { Component } from 'react'; import { render } from 'react-do ...

Utilizing Mongoose.js to nest a find() query within another find() query

Can I perform a nested search on Node using Mongoose? I need to update a user's email address, but first I have to ensure that the new email is not already associated with another user. This is what I want to achieve: /***************************** ...

Is there a way to trigger this floating menu to appear only when certain #divs are in view?

I was looking to create a floating menu and came across a helpful script on here /* Script by: www.jtricks.com * Version: 1.12 (20120823) * Latest version: www.jtricks.com/javascript/navigation/floating.html * * License: * GNU/GPL v2 or later http:// ...

How can one create custom JavaScript UI controls for a UWP application?

I am currently working on a UWP application and I am looking to incorporate JavaScript HTML5 based UI components. Is there a method to integrate this UI into a UWP application? My UI is completely developed using JavaScript, jQuery, CSS, and HTML5, and I w ...

Exploring the advanced features of OpenOffice Draw for improved geometry analysis

Struggling with the draw:enhanced-geometry section that involves draw:enhanced-path and draw:equation. I'm working on an OOo converter but can't seem to find any concrete solutions or extensive documentation about this part. Any suggestions on ho ...

The error message "TypeError: usert.addItem is not a function" indicates that

Currently in the process of developing a discord bot using discord.js, sequelize, and sqlite for database management. Encountering an issue with a custom function that is not being recognized as defined by the terminal, despite me confirming its definition ...

Issue with Node Webpack identifying the "Import" statement

I'm diving into the world of Node and Webpack, but I'm struggling with getting my project to compile properly. Every time I try to load it in the browser, I encounter the error message: Uncaught SyntaxError: Unexpected token import. Let me share ...

Tips on adjusting the Leaflet Map's zoom level to display all markers in React Leaflet

I am currently working on a project with React Leaflet map that requires changing the center and zoom based on a set of markers. The goal is to adjust the zoom level so that all the markers are visible on the map. To achieve this change in view, I am util ...

Tips for hovering over a link with Webdriver

Currently, for my project, I am utilizing Selenium Webdriver. Successfully automating the functionality to mouse over an image has been achieved. However, there seems to be an issue when attempting to trigger a mouse-over event on a hyperlink using the sam ...

Mastering the placement of lights in ThreeJS

Struggling for nearly half an hour now, attempting to place a pointlight at the base of my model, but achieving dismal outcomes. Not sure of the dimensions of my model, and consistently failing to accurately pinpoint the light within the scene. Thought ab ...

Tips for clearing fields and displaying a success message after submitting a form with Vue.js

I am a beginner in Vue.js and I am facing an issue with my form submission. Even though the data is being saved successfully, the form fields are not getting emptied after submitting and there is no success message displayed. Can someone please provide gui ...

What is the best way to display a selected checkbox in an AJAX editing modal?

Can someone assist me in loading the checked checkboxes in an edit modal? I am using checkboxes to assign multiple roles in my system. Everything was working fine, but I encountered an issue with my edit modal where the checkboxes appear blank and unchecke ...

Is there a way to display a specific dropdown menu depending on the checkbox that is selected?

I have a bunch of checkbox items, including one labeled nocalls, as well as a couple of dropdownlist boxes. Here are the dropdown boxes: <tr> <td align="right"><FONT class="Arial10"><B>Profile<font color="#ff0000">*</ ...

Continue looping in Javascript until an empty array is identified

Currently, I am in search of a solution to create a loop in Javascript that continues until the array of objects is empty. The object I am working with looks like this: "chain": { "evolves_to": [{ "evolves_to": [{ ...

Could anyone assist me in finding a way to exclude a particular "class" from being iterated over while utilizing ng-repeat in Angularjs?

Utilizing AngularJS within the framework of my Bootstrap carousel has presented a challenge. When I implement the directive ng-repeat="album in ActiveSinger.albums" to loop through my array, it continuously adds the class "active" to each div element. This ...