mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-09 20:12:10 +10:00
* feat: set lang in html head * fix: add # in front of git ref * fix: remove unused vars from example env * fix: package name and license field * fix: enable sourcemap for client and server * fix: emojis not showing in prod this is extremely cursed, but it works * chore: refactor auth manager * feat: disable invitations if simple auth disabled * feat: add drop version to footer * feat: translate auth endpoints * chore: move oidc module * feat: add weekly tasks enabled object cleanup as weekly task * feat: add timestamp to task log msgs * feat: add guard to prevent invalid progress % * fix: add missing global scope to i18n components * feat: set base url for i18n * feat: switch task log to json format * ci: run ci on develop branch only * fix: UserWidget text not updating #109 * fix: EXTERNAL_URL being computed at build * feat: add basic language outlines for translation * feat: add more english dialects
95 lines
2.6 KiB
TypeScript
95 lines
2.6 KiB
TypeScript
import { AuthMec } from "~/prisma/client";
|
|
import prisma from "~/server/internal/db/database";
|
|
import authManager, { createHashArgon2 } from "~/server/internal/auth";
|
|
import * as jdenticon from "jdenticon";
|
|
import objectHandler from "~/server/internal/objects";
|
|
import { type } from "arktype";
|
|
import { randomUUID } from "node:crypto";
|
|
import { throwingArktype } from "~/server/arktype";
|
|
|
|
export const CreateUserValidator = type({
|
|
invitation: "string?", // Optional because we re-use this validator
|
|
username: "string >= 5",
|
|
email: "string.email",
|
|
password: "string >= 14",
|
|
"displayName?": "string | undefined",
|
|
}).configure(throwingArktype);
|
|
|
|
export default defineEventHandler<{
|
|
body: typeof CreateUserValidator.infer;
|
|
}>(async (h3) => {
|
|
const t = await useTranslation(h3);
|
|
|
|
if (!authManager.getAuthProviders().Simple)
|
|
throw createError({
|
|
statusCode: 403,
|
|
statusMessage: t("errors.auth.method.signinDisabled"),
|
|
});
|
|
|
|
const user = await readValidatedBody(h3, CreateUserValidator);
|
|
|
|
const invitationId = user.invitation;
|
|
if (!invitationId)
|
|
throw createError({
|
|
statusCode: 401,
|
|
statusMessage: t("errors.auth.invalidInvite"),
|
|
});
|
|
|
|
const invitation = await prisma.invitation.findUnique({
|
|
where: { id: invitationId },
|
|
});
|
|
if (!invitation)
|
|
throw createError({
|
|
statusCode: 401,
|
|
statusMessage: t("errors.auth.invalidInvite"),
|
|
});
|
|
|
|
// reuse items from invite
|
|
if (invitation.username !== null) user.username = invitation.username;
|
|
if (invitation.email !== null) user.email = invitation.email;
|
|
|
|
const existing = await prisma.user.count({
|
|
where: { username: user.username },
|
|
});
|
|
if (existing > 0)
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: t("errors.auth.usernameTaken"),
|
|
});
|
|
|
|
const userId = randomUUID();
|
|
|
|
const profilePictureId = randomUUID();
|
|
await objectHandler.createFromSource(
|
|
profilePictureId,
|
|
async () => jdenticon.toPng(user.username, 256),
|
|
{},
|
|
[`internal:read`, `${userId}:read`],
|
|
);
|
|
const [linkMec] = await prisma.$transaction([
|
|
prisma.linkedAuthMec.create({
|
|
data: {
|
|
mec: AuthMec.Simple,
|
|
credentials: await createHashArgon2(user.password),
|
|
version: 2,
|
|
user: {
|
|
create: {
|
|
id: userId,
|
|
username: user.username,
|
|
displayName: user.displayName ?? user.username,
|
|
email: user.email,
|
|
profilePictureObjectId: profilePictureId,
|
|
admin: invitation.isAdmin,
|
|
},
|
|
},
|
|
},
|
|
select: {
|
|
user: true,
|
|
},
|
|
}),
|
|
prisma.invitation.delete({ where: { id: invitationId } }),
|
|
]);
|
|
|
|
return linkMec.user;
|
|
});
|