Add a role
One page for every option
This page covers every choice of --Persistence, --Auth, --IncludeTests and --Aspire. A label says which options a part is for; the copy in your app's docs/ folder has only yours. ApiHost stands for your app's name.
Roles are constants in src/ApiHost/Identity/AppRoles.cs. The app creates them in the database when it starts, and the admin page offers every role in AppRoles.All as a checkbox.
Add the role:
csharppublic static class AppRoles { public const string Admin = "Admin"; public const string Editor = "Editor"; public static IReadOnlyList<string> All { get; } = [Admin, Editor]; }Protect endpoints with it:
[Authorize(Roles = AppRoles.Editor)]. Several roles are allowed with a comma:[Authorize(Roles = $"{AppRoles.Admin},{AppRoles.Editor}")].In the client, check the role from
useMe()(me.data?.roles.includes('Editor')) to show or hide links and pages, asRequireAdmininfeatures/auth/routeGuards.tsxdoes for the admin page. The API's check is what protects the data; the client's only tidies the interface.Restart the app, then give people the role on the admin page. It reaches their access token at their next token refresh (within a few minutes) or login.
- With the tests Add a test that the endpoint refuses users without the role (403), like
only_admins_can_use_the_admin_endpointsinAdminEndpointsTests.cs.
Removing a role from AppRoles doesn't delete it from the database or from users; remove it from users on the admin page first.