401 (Unauthorized) when trying to update user data

System Information
  • Strapi Version: 4.11.0
  • Operating System: MacOS
  • Database: MySQL
  • Node Version:
  • NPM Version:
  • Yarn Version:

I have managed to successful create external registration and log in forms, which utilise the Strapi API. Data is successfully stored within Strapi.

I am working on a page which allows users to update their details, such as first and last names, email address, and password. I have found myself stuck on the updating of stored details, which produce a ‘Failed to load resource: the server responded with a status of 401 (Unauthorized)’ error within the browser console.

Could anyone point me in the right direction with this? :grin:

      const response = await axios.put('http://localhost:1337/api/users/me', {
        firstName: updatedFirstName,
        lastName: updatedLastName,
      }, {
        headers: {
          Authorization: `Bearer ${cookies.jwt}`,
        },
      });

Have you tried console log the jwt to make sure it exists.

I also don’t think you can’t do an update on /me. You have to do /users/X
where X is the id of the user.

Thanks for your response.

The jwt is stored as a cookie. I has successfully logged in and the API is pulling the stored first and last name correctly. I’ve also tried with a user ID and it’s still giving me a ‘401 (Unauthorized)’ error.

Have you given the correct permissions in the user-permissions admin panel?

I am. I’m not sure what else it could be?

All I need is a page which allows users to update details.

Hi @pullbear12,
I think you just cannot use “PUT” on endpoint /users/me since this enpoint is just allowed for “GET”

You need to create a custom one for this purpose.
See here: Updating Your Own User Info in Strapi - YouTube
or here: Users me update

I agree hence why i posted here

Thank you for your responses, they have been a huge help. :slight_smile:

The JWT token was not being corrected past along as part of the authorisation process. This has been corrected.

1 Like

I am having the same issue but using the Strapi helper

If I run the following code from my plugin while BEING LOGGED_IN in admin panel:

import { getFetchClient } from '@strapi/helper-plugin';

const { get } = getFetchClient();
const response = await get('/api/myendpoint');

I do get 401 response and logged-out…

I see in the network tab that the request has bearer token in Auth header