mirror of
https://github.com/Drop-OSS/drop.git
synced 2026-06-22 04:11:32 +10:00
* fix: server side validation and client side validation for invitation creation * fix: lint
This commit is contained in:
@@ -360,6 +360,9 @@
|
|||||||
:loading="loading"
|
:loading="loading"
|
||||||
type="submit"
|
type="submit"
|
||||||
class="w-full sm:w-fit"
|
class="w-full sm:w-fit"
|
||||||
|
:disabled="
|
||||||
|
!(validUsername && validEmail && username && email)
|
||||||
|
"
|
||||||
>
|
>
|
||||||
{{ $t("users.admin.simple.inviteButton") }}
|
{{ $t("users.admin.simple.inviteButton") }}
|
||||||
</LoadingButton>
|
</LoadingButton>
|
||||||
|
|||||||
@@ -1,18 +1,14 @@
|
|||||||
import { type } from "arktype";
|
|
||||||
import { readDropValidatedBody, throwingArktype } from "~/server/arktype";
|
import { readDropValidatedBody, throwingArktype } from "~/server/arktype";
|
||||||
import aclManager from "~/server/internal/acls";
|
import aclManager from "~/server/internal/acls";
|
||||||
import prisma from "~/server/internal/db/database";
|
import prisma from "~/server/internal/db/database";
|
||||||
|
import { CreateUserValidator } from "../../../auth/signup/simple.post";
|
||||||
|
|
||||||
const CreateInvite = type({
|
const CreateInvite = CreateUserValidator.and({
|
||||||
isAdmin: "boolean?",
|
expires: "Date",
|
||||||
username: "string?",
|
isAdmin: "boolean = false",
|
||||||
email: "string.email?",
|
|
||||||
expires: "string.date.iso.parse",
|
|
||||||
}).configure(throwingArktype);
|
}).configure(throwingArktype);
|
||||||
|
|
||||||
export default defineEventHandler<{
|
export default defineEventHandler(async (h3) => {
|
||||||
body: typeof CreateInvite.infer;
|
|
||||||
}>(async (h3) => {
|
|
||||||
const allowed = await aclManager.allowSystemACL(h3, [
|
const allowed = await aclManager.allowSystemACL(h3, [
|
||||||
"auth:simple:invitation:new",
|
"auth:simple:invitation:new",
|
||||||
]);
|
]);
|
||||||
@@ -20,6 +16,8 @@ export default defineEventHandler<{
|
|||||||
|
|
||||||
const body = await readDropValidatedBody(h3, CreateInvite);
|
const body = await readDropValidatedBody(h3, CreateInvite);
|
||||||
|
|
||||||
|
console.log(body);
|
||||||
|
|
||||||
const invitation = await prisma.invitation.create({
|
const invitation = await prisma.invitation.create({
|
||||||
data: body,
|
data: body,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,21 +5,22 @@ import * as jdenticon from "jdenticon";
|
|||||||
import objectHandler from "~/server/internal/objects";
|
import objectHandler from "~/server/internal/objects";
|
||||||
import { type } from "arktype";
|
import { type } from "arktype";
|
||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
|
import { throwingArktype } from "~/server/arktype";
|
||||||
|
|
||||||
const userValidator = type({
|
export const CreateUserValidator = type({
|
||||||
invitation: "string",
|
invitation: "string?", // Optional because we re-use this validator
|
||||||
username: "string >= 5",
|
username: "string >= 5",
|
||||||
email: "string.email",
|
email: "string.email",
|
||||||
password: "string >= 14",
|
password: "string >= 14",
|
||||||
"displayName?": "string | undefined",
|
"displayName?": "string | undefined",
|
||||||
});
|
}).configure(throwingArktype);
|
||||||
|
|
||||||
export default defineEventHandler<{
|
export default defineEventHandler<{
|
||||||
body: typeof userValidator.infer;
|
body: typeof CreateUserValidator.infer;
|
||||||
}>(async (h3) => {
|
}>(async (h3) => {
|
||||||
const body = await readBody(h3);
|
const user = await readValidatedBody(h3, CreateUserValidator);
|
||||||
|
|
||||||
const invitationId = body.invitation;
|
const invitationId = user.invitation;
|
||||||
if (!invitationId)
|
if (!invitationId)
|
||||||
throw createError({
|
throw createError({
|
||||||
statusCode: 401,
|
statusCode: 401,
|
||||||
@@ -35,17 +36,6 @@ export default defineEventHandler<{
|
|||||||
statusMessage: "Invalid or expired invitation.",
|
statusMessage: "Invalid or expired invitation.",
|
||||||
});
|
});
|
||||||
|
|
||||||
const user = userValidator(body);
|
|
||||||
if (user instanceof type.errors) {
|
|
||||||
// hover out.summary to see validation errors
|
|
||||||
console.error(user.summary);
|
|
||||||
|
|
||||||
throw createError({
|
|
||||||
statusCode: 400,
|
|
||||||
statusMessage: user.summary,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// reuse items from invite
|
// reuse items from invite
|
||||||
if (invitation.username !== null) user.username = invitation.username;
|
if (invitation.username !== null) user.username = invitation.username;
|
||||||
if (invitation.email !== null) user.email = invitation.email;
|
if (invitation.email !== null) user.email = invitation.email;
|
||||||
|
|||||||
Reference in New Issue
Block a user