Place the token within the Nuxt auth-module

Working on a project using the Nuxt auth-module. The Login API response is structured like this:

data:{
    data:{
        user:{
            bio: null,
            createdAt: "2021-06-29T12:28:42.442Z",
            email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a1cfc0ccc4e1c8cfc7ce8fc2ce">[email protected]</a>",
            id: 1,
            image: null,
            token: "token",
            updatedAt: "2021-06-29T12:28:42.447Z",
            username: "name"
        }
     }
} 

nuxtconfig:

  auth: {
    strategies: {
      local: {
        endpoints: {
          login: { url: 'users/login', method: 'post', propertyName: 'data.user.token' },
          user: { url: 'me', method: 'get', propertyName: 'data' },
          logout: false
        }
      }
    }
  },

Having trouble saving the token in the app. Any suggestions?

After some research, I came across a solution:

  auth: {
    strategies: {
      local: {
        token: {
          property: 'user.token', // /user endpoint API returns user object
          type: 'Token' // if your token type is not Bearer
        },
        user: {
          user: 'user'
        },
        endpoints: {
          login: { url: '/users/login', method: 'post' },
          user: { url: '/user', method: 'get' },
          logout: false
        }
      }
    }
  },

Answer №1

The structure you're looking for is data.data.user and not just data.user. Therefore, in my opinion, you will need to define it in the configuration like this:

auth: {
  strategies: {
    local: {
      user: {
        property: 'data.user', // or possibly 'data.data.user'
        // autoFetch: true
      },
    }
  }
}

This information can be found in the official documentation.

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

``A Vue component featuring a visually appealing table displaying a list of multiple arrays, each

Is it possible to render a table with an Object like this? list { Array[1] = [ Object[0] = { name: "Hello world" } ], Array[2] = [ Object[0] = { name: "Hello world" }, Object[1] = { name: "Hello world" } ...

What is the best way to switch between search results shown in an li using AngularJS?

Looking for a way to toggle a list that appears when a user searches in my app. I want the search results to hide when the search bar is closed. How can I achieve this? I think Angular might be the solution, but I'm stuck on how to implement it. I tri ...

Using jQuery to handle events across multiple elements

Could I achieve something similar to this? I currently have several variables assigned to DOM elements. Rather than querying the DOM again to set event handlers, I would like to utilize the variables I already have. var a = $("#foo"); var b = $("#bar"); ...

Trouble with AJAX GET request functionality

I am new to AJAX and attempting to make my first AJAX call. Here is the code I have so far: $.get( "validate.php", { 'userinput':'x'}, function(response) { if( response.status ) alert( "Matches found" ); else alert( "No matches ...

Identifying with a jQuery IF statement all input fields that contain null values in order to eliminate the respective elements

My goal is to create a jQuery script that checks all input fields to see if they are empty. If an input field is empty, I want to remove the child image of the next occurring span element. Here is what I have attempted so far: if ($("input").val() == "") ...

By default, make the initial element of the list the selected option in an AngularJS select input

I'm running into an issue with setting the first element in my ng-repeat list within a select input. Here is the code snippet causing the problem: <div> <span >OF</span> <select ng-model="eclatementCourante.ordreFabricationId" n ...

Highchart tip: How to create a scrollable chart with only one series and update the x-axis variable through drilldown

Before I pose my question, here is a link to my jsfiddle demo: http://jsfiddle.net/woon123/9155d4z6/1/ $(document).ready(function () { $('#deal_venue_chart').highcharts({ chart: { type: 'column' ...

Why isn't the unit test passing when it clearly should be?

I am encountering an issue with testing a simple function that I have created. Despite the fact that the function works correctly in practice, it is not being tested properly... Explanation of how my function operates (While it functions as intended, the ...

No values are being assigned to the array elements in the Node.js application

I've been struggling with an issue in my Express project for a day now. I can't seem to push some data into an array element. Let me walk you through my code and the data involved. Here is the result data retrieved from MongoDB: result = { n ...

How to implement the ECharts animated bar chart in Angular version 16?

The animated bar chart in ECharts functions perfectly on Stackblitz. You can check it out here in the Stackblitz Angular 16 demo. However, attempting to run the same demo in a local Angular 16 project led to the following errors. Error: src/app/animated- ...

Refresh page upon clicking the same state link in AngularJS using ui.router

Just received an interesting request from our QA team that seems a bit out there. Let's dive in: imagine you are already on the 'about' state/page within an angular-based app, and when you click on the 'about' state URL again from ...

Error: Attempting to access 'title' property of undefined object leads to Uncaught TypeError

I am attempting to extract content from a Google Blogger Feed that can be found at this link. I am using JavaScript code from here. When inspecting the elements, I encountered a specific red warning message: Uncaught TypeError: Cannot read property ' ...

I am currently attempting to extract data from a JSON file by using key names for reference, but I am running into issues when dealing with nested keys

Is there a better way to retrieve values from a JSON file by matching key names? The current method I am using does not seem to work with nested keys, so any suggestions on alternative approaches would be appreciated. // Sample .JSON file { "ro ...

"Troubleshooting Issue: Why Props in mapStateToProps Aren't Updating After Running an Action in Redux

Adding some context, I utilize Redux to manage updates in a shopping cart list. Essentially, I retrieve the list, add an item to it, and then update the entire list. However, after executing an action, my cartList does not seem to update. Despite consulti ...

Display the matrix in a semi-spiral pattern

I am working with a 3 by 5 matrix, filled with numbers from 1, presented as follows: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 The task is to print the values in a half-spiral order, starting from the bottom left vertically: 11 6 1 5 10 15 12 7 2 4 9 ...

The testing for React and TypeScript did not succeed. It is recommended to utilize the "jsdom" test environment for better results

I'm working on a basic React/TypeScript project and diving into the world of testing. I've opted for React Testing Library and Jest to test a straightforward product page that should display the message "Welcome to our product page." Unfortunate ...

Mozilla browser experiencing issues with mouse move event functionality

Here, I have a background image on the body and with the following script, when the user moves the mouse, the image in the background also moves. body { background-image: url('../images/1.png'); background-size: 98%; background-posi ...

Having trouble getting my javascript integration to work in a Django template - any ideas on what could be causing

As a newcomer to Django, I'm working on creating a small webpage that displays a chart using Chart.js. My goal is to load the JavaScript file statically. To achieve this, I have set up a basic HTML file named data_table.html and included a "static" fo ...

Accessing data from arrays asynchronously using JavaScript

Update I have included actual code below, in addition to the concept provided earlier. Here is the async array access structure I am trying to implement: for (p = 0; p < myList.length ; p++){ for (k = 0; k < RequestList.length; k++){ i ...

Updating the style sheet of a selected menu item on an ASP.NET master page

I created an asp.net master page with a menu setup like this: <menu id="menu"> <nav id="main_nav"> <ul id="menu-primary"> <li ><a href="./">Home</a></li> <li><a href="staff.aspx"& ...