Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: User UserCreationService in self serve admin creation #19200

Open
wants to merge 9 commits into
base: create-usercreationservice-for-api
Choose a base branch
from
28 changes: 11 additions & 17 deletions apps/web/pages/api/auth/setup.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { NextApiRequest } from "next";
import z from "zod";

import { hashPassword } from "@calcom/features/auth/lib/hashPassword";
import { isPasswordValid } from "@calcom/features/auth/lib/isPasswordValid";
import { emailRegex } from "@calcom/lib/emailSchema";
import { HttpError } from "@calcom/lib/http-error";
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
import slugify from "@calcom/lib/slugify";
import { UserCreationService } from "@calcom/lib/server/service/userCreationService";
import prisma from "@calcom/prisma";
import { IdentityProvider } from "@calcom/prisma/enums";
import { CreationSource } from "@calcom/prisma/enums";
Expand Down Expand Up @@ -34,23 +33,18 @@ async function handler(req: NextApiRequest) {
throw new HttpError({ statusCode: 422, message: parsedQuery.error.message });
}

const username = slugify(parsedQuery.data.username.trim());
const userEmail = parsedQuery.data.email_address.toLowerCase();

const hashedPassword = await hashPassword(parsedQuery.data.password);
Comment on lines -37 to -40
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move these two calls to the UserCreationService


await prisma.user.create({
data: {
username,
email: userEmail,
password: { create: { hash: hashedPassword } },
role: "ADMIN",
name: parsedQuery.data.full_name,
emailVerified: new Date(),
locale: "en", // TODO: We should revisit this
identityProvider: IdentityProvider.CAL,
creationSource: CreationSource.WEBAPP,
},
await UserCreationService.createUser({
username: parsedQuery.data.username.trim(),
email: userEmail,
password: parsedQuery.data.password,
role: "ADMIN",
name: parsedQuery.data.full_name,
emailVerified: new Date(),
locale: "en", // TODO: We should revisit this
identityProvider: IdentityProvider.CAL,
creationSource: CreationSource.SELF_SERVE_ADMIN,
});

return { message: "First admin user created successfully." };
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterEnum
ALTER TYPE "CreationSource" ADD VALUE 'self_serve_admin';
7 changes: 4 additions & 3 deletions packages/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ enum PeriodType {
}

enum CreationSource {
API_V1 @map("api_v1")
API_V2 @map("api_v2")
WEBAPP @map("webapp")
API_V1 @map("api_v1")
API_V2 @map("api_v2")
WEBAPP @map("webapp")
SELF_SERVE_ADMIN @map("self_serve_admin")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should expand on the CreationSource to pinpoint where watchlisted users are created

}

model Host {
Expand Down