I’m currently working on a NextJS / TypeScript project and I am trying to create users via the Management API (i.e sending a POST
req to the /api/v2/users
endpoint).
I have basically granted full privileges (for testing purposes, will later sort it out) to my M2M application. I am successfully getting an accessToken
and I am actually able to sucessfully create a dummy user via Postman with pretty much the same configuration as the one which I’m using for my back-end.
const createUsers = async (req: any, res: any) => {
let accessToken;
const data = {
email: "john.doe@gmail.com",
blocked: false,
email_verified: false,
given_name: "John",
family_name: "Doe",
name: "John Doe",
nickname: "Johnny",
user_id: "abc",
connection: "Username-Password-Authentication",
password: "Test12371&!@&891",
verify_email: false,
username: "johndoe",
};
try {
const response = await axios.get(
`http://localhost:3000/api/authentication/getAccessToken`
);
accessToken = response.data.access_token;
} catch (error) {
res.status(500).json(error);
}
try {
if (!accessToken) throw new Error("No access token found");
console.log(accessToken);
const response = await axios.post(
`https://${domain}.eu.auth0.com/api/v2/users`,
{
method: "POST",
headers: {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify(data),
}
);
res.status(200).json(response.data);
} catch (error) {
console.error(error);
res.status(500).json(error);
}
};
The accessToken is fetched without any issues, however, I assume that for some reason it’s not being sent as a header(?).
I am using literally the same structure for fetching a list of all users, (GET req.) and over there it works without any problems.
Part of the error which I see is:
data: {
statusCode: 401,
error: 'Unauthorized',
message: 'Missing authentication'
}
More of it:
Postman request: (Using the same accessToken which is printed by my console.log)
I’m assuming that this is most likely something quite dumb, but I’ve been unable to see it for the past few hours.