1

I wrote a little login register tool with nuxt and everything works fine but if im changing the data with a put request, nuxt auth dont "see" the change and stays with outdated data.

In my Dashboard the user data is shown by:

<div id="userInfos">
        Name: {{ this.$auth.user.firstname }}, {{ this.$auth.user.lastname }} <br />
        Username: {{ this.user.username }} <br />
</div>

My put request is like that (wrote it in a javascript helper file)

export async function changeData(id, body) {
    try {
        await axios.put(`http://localhost:8000/users/${id}`, body, {
            headers: {
                'Content-Type': 'application/json',
                'Authorization': localStorage.getItem('auth._token.local')
            }
        });
        return true;
    }
    catch (e) {
        return false;
    }
}

I already tried things like refreshTokens() but there was also not change. The data is only changing after a logout and login again. Is there a way to update the this.$auth.user data updated after changing it?

//EDIT Now im trying to get the new data from my backend to override the $auth.user. But it still sends me the old data which dont exist on the backend anymore?

My function:

async getData() {
      this.$auth.refreshTokens()
      try {
        let res = await this.$axios.get(`http://localhost:8000/userinfo/`, {
          headers: {
            'Content-Type': 'application/json',
            Authorization: localStorage.getItem('auth._token.local'),
          },
        })
        console.log(res.data)
      } catch (e) {
        console.log(e)
      }
    },
    
//Starts in
beforeMount() {
    this.getData()
  },

And my backend code where i ask for the data:

server.get("/userinfo", (req, res) => {
  const decodedToken = jwt.decode(req.headers.authorization.split(' ')[1])
  const userInfo = {
    id: decodedToken.id,
    firstname: decodedToken.firstname,
    lastname: decodedToken.lastname,
    username: decodedToken.username,
    roles: decodedToken.roles,
    password: decodedToken.password,
  }

  res.status(200).json(userInfo)
})

Seems like there is something wrong with the token? The backend shows me also a 304 error code

Chopper
  • 63
  • 7

1 Answers1

0

Fixed it myself, problem was in the backend. Needed to read the json file in my backend correct like this:

const userList = JSON.parse(fs.readFileSync('./users.json', 'UTF-8'))
const userinfo = userList.users.find((user) => user.id === decodedToken.id)
kissu
  • 40,416
  • 14
  • 65
  • 133
Chopper
  • 63
  • 7