I was browsing though the Umbraco documentation, but sadly wasn’t able to find any useful help for my situation (maybe I didn’t search in the correct places, in that case sorry!)
We’re currently running an Umbraco 16 project for our client. We want to create a custom dashboard to trigger an import from within the Umbraco Backoffice (content section, custom dashboard was the idea).
Currently I have two significant files in my project (custom dashboard and custom backoffice api endpoint) and I was wondering if anyone can help me figure out how I can send the authentication from the logged in user to the backoffice, making it possible to trigger the custom backoffice endpoint with the logged in user to authorize the request.
Here’s my custom dashboard (using Lit):
import { LitElement, css, html } from 'lit'
import { customElement, property } from 'lit/decorators.js'
@customElement('import-dashboard')
export class ImportDashboard extends LitElement {
@property({ type: Number })
responseMessage = '';
render() {
return html`
<div class="card">
<button @click=${this.callApi} part="button">Start Import</button>
<div>${this.responseMessage}</div>
</div>
`
}
private async callApi() {
this.responseMessage = 'Import started...';
try {
const res = await fetch('/umbraco/backoffice/api/import/startImport', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
});
const json = await res.json();
this.responseMessage = `Import finished: ${json}`;
} catch(err: any) {
this.responseMessage = `Error: ${err}`;
}
}
}
declare global {
interface HTMLElementTagNameMap {
'import-dashboard': ImportDashboard
}
}
And here’s my custom backoffice api endpoint:
[ApiController]
[EndpointGroupName("Import")]
[Route("/umbraco/backoffice/api/import")]
[JsonOptionsName(Constants.JsonOptionsNames.DeliveryApi)]
public class ForceManualImportController(IImportService importService, IConfiguration configuration, ILogger<ForceManualImportController> logger) : UmbracoAuthorizedController
{
[HttpPost]
[Route("startImport")]
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
[Authorize(Roles = "admin")]
public async Task<IActionResult> StartImport()
{
logger.LogInformation("Starting import");
var importSettings = new ImportSettings
{
AlgoliaUpdate = true,
ImportAdvisors = true,
ImportCategories = true,
ImportArrangements = true,
ImportCourses = true,
ImportLocations = true,
ImportSchedules = true,
ImportStudies = true,
ImportTeachers = true,
IndexLocationForStudies = true,
};
if (await importService.Import(importSettings))
{
logger.LogInformation("Import succesful");
return Ok("Import succesful");
}
logger.LogWarning("Import aborted");
return Ok("Import is aborted.");
}
}
If I click the button on the dashboard, it keeps telling me 401 Unauthorized, which makes me think that I should add some sort of an authentication header, but I have no clue where to grab the currently logged in user information for the authentication to be completed.
Any pointers or people who have ran into the same issue and were able to fix it?
Thanks in advance!