The problem is, when this .ts file loads, I immediately get redirected to the sign-in page: “Your session has timed out. Please sign in again below.”. If I sign in, then after 1 second, I get redirected again, and so on.
I also tried just using “fetch”, but then I got a 401 Unauthorized error.
What I want to achieve is that the backoffice login should be sufficient to be authorized to this API.
What should I do in my .ts file to call my controller without any issues?
I did some digging, and I refactored my code, so I moved the sections and sectionView (and the newly added entry point) from the umbraco.package.json to proper mainfest.ts files and bundled them, only adding the bundle to the umbraco.package.json.
After that, my problem was that I started digging into the template’s generated API folder, and I mixed the generated API client with my umbHttpClient, so it didn’t work. But then I realized this and deleted the generated API client and just used umbHttpClient, and I used it in the entrypoint too.
And at the end, this piece of your code fixed the problem:
// For every request being made, add the token to the headers
// Can't use the setConfig approach above as its set only once and
// tokens expire and get refreshed
client.interceptors.request.use(async (request, _options) => {
const token = await config.token();
request.headers.set('Authorization', `Bearer ${token}`);
return request;
});