Refactor demo login as an authentication method

Use the authentication method system for the demo login and the
generated accounts.  This makes it possible to toggle it off on
production systems as these shouldn't have it enabled at all.
This commit is contained in:
Hornwitser 2025-07-09 17:57:49 +02:00
parent a33c8e9dac
commit 0d0e38e4b6
14 changed files with 212 additions and 141 deletions

View file

@ -15,8 +15,8 @@ export default defineEventHandler(async (event): Promise<ApiSession> => {
});
}
const formData = await readBody(event);
const name = formData.name;
const body = await readBody(event);
const name = body?.name;
const users = await readUsers();
let user: ServerUser;
@ -42,7 +42,7 @@ export default defineEventHandler(async (event): Promise<ApiSession> => {
name,
};
} else if (name === null) {
} else if (name === undefined) {
user = {
id: await nextUserId(),
updatedAt: new Date().toISOString(),
@ -55,7 +55,14 @@ export default defineEventHandler(async (event): Promise<ApiSession> => {
});
}
if (session?.authenticationProvider) {
if (user.type !== "anonymous") {
if (!session?.authenticationProvider) {
throw createError({
statusCode: 409,
statusMessage: "Conflict",
message: "User account need an authentication method associated with it.",
});
}
const authMethods = await readAuthenticationMethods();
const method = authMethods.find(method => (
method.provider === session.authenticationProvider