Self-hosted onboarding first admin wizard (#3393)
* WIP * API and step done fallback * Finishing up tweaks * Inline comment * Translations
This commit is contained in:
1 parent
c93e238e15
commit
ee14423f4c
7 files changed
+492
-1
No files matched your search
@@ -0,0 +1,58 @@
|
||||
import { IdentityProvider } from "@prisma/client";
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import z from "zod";
|
||||
|
||||
import { isPasswordValid } from "@calcom/lib/auth";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
|
||||
|
||||
import { hashPassword } from "@lib/auth";
|
||||
import prisma from "@lib/prisma";
|
||||
import slugify from "@lib/slugify";
|
||||
|
||||
const querySchema = z.object({
|
||||
username: z.string().min(1),
|
||||
fullname: z.string(),
|
||||
email: z.string().email({ message: "Please enter a valid email" }),
|
||||
password: z.string().refine((val) => isPasswordValid(val.trim()), {
|
||||
message:
|
||||
"The password must be a minimum of 7 characters long containing at least one number and have a mixture of uppercase and lowercase letters",
|
||||
}),
|
||||
});
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const userCount = await prisma.user.count();
|
||||
if (userCount !== 0) {
|
||||
throw new HttpError({ statusCode: 400, message: "No setup needed." });
|
||||
}
|
||||
|
||||
const parsedQuery = querySchema.safeParse(req.body);
|
||||
if (!parsedQuery.success) {
|
||||
throw new HttpError({ statusCode: 422, message: parsedQuery.error.message });
|
||||
}
|
||||
|
||||
const username = slugify(parsedQuery.data.username);
|
||||
const userEmail = parsedQuery.data.email.toLowerCase();
|
||||
|
||||
const hashedPassword = await hashPassword(parsedQuery.data.password);
|
||||
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
username,
|
||||
email: userEmail,
|
||||
password: hashedPassword,
|
||||
role: "ADMIN",
|
||||
name: parsedQuery.data.fullname,
|
||||
emailVerified: new Date(),
|
||||
locale: "en", // TODO: We should revisit this
|
||||
plan: "PRO",
|
||||
identityProvider: IdentityProvider.CAL,
|
||||
},
|
||||
});
|
||||
|
||||
res.status(201).json({ message: "First admin user created successfuly." });
|
||||
}
|
||||
|
||||
export default defaultHandler({
|
||||
POST: Promise.resolve({ default: defaultResponder(handler) }),
|
||||
});
|
||||
@@ -8,13 +8,14 @@ import { useState } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
|
||||
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { Alert } from "@calcom/ui/Alert";
|
||||
import Button from "@calcom/ui/Button";
|
||||
import { EmailField, Form, PasswordField } from "@calcom/ui/form/fields";
|
||||
import prisma from "@calcom/web/lib/prisma";
|
||||
|
||||
import { ErrorCode, getSession } from "@lib/auth";
|
||||
import { WEBAPP_URL, WEBSITE_URL } from "@lib/config/constants";
|
||||
import { useLocale } from "@lib/hooks/useLocale";
|
||||
import { hostedCal, isSAMLLoginEnabled, samlProductID, samlTenantID } from "@lib/saml";
|
||||
import { collectPageParameters, telemetryEventTypes, useTelemetry } from "@lib/telemetry";
|
||||
import { inferSSRProps } from "@lib/types/inferSSRProps";
|
||||
@@ -217,6 +218,17 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
|
||||
};
|
||||
}
|
||||
|
||||
const userCount = await prisma.user.count();
|
||||
if (userCount === 0) {
|
||||
// Proceed to new onboarding to create first admin user
|
||||
return {
|
||||
redirect: {
|
||||
destination: "/auth/setup",
|
||||
permanent: false,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
props: {
|
||||
csrfToken: await getCsrfToken(context),
|
||||
|
||||
@@ -0,0 +1,293 @@
|
||||
import { CheckIcon } from "@heroicons/react/solid";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { useRouter } from "next/router";
|
||||
import { Controller, useForm } from "react-hook-form";
|
||||
import * as z from "zod";
|
||||
|
||||
import { isPasswordValid } from "@calcom/lib/auth";
|
||||
import classNames from "@calcom/lib/classNames";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { inferSSRProps } from "@calcom/types/inferSSRProps";
|
||||
import WizardForm from "@calcom/ui/WizardForm";
|
||||
import { Input } from "@calcom/ui/form/fields";
|
||||
import { Form } from "@calcom/ui/form/fields";
|
||||
|
||||
import prisma from "@lib/prisma";
|
||||
|
||||
const schema = z.object({
|
||||
username: z.string().min(1),
|
||||
email: z.string().email({ message: "Please enter a valid email" }),
|
||||
fullname: z.string(),
|
||||
password: z.string().refine((val) => isPasswordValid(val.trim()), {
|
||||
message:
|
||||
"The password must be a minimum of 7 characters long containing at least one number and have a mixture of uppercase and lowercase letters",
|
||||
}),
|
||||
});
|
||||
|
||||
const StepDone = () => {
|
||||
const { t } = useLocale();
|
||||
|
||||
return (
|
||||
<div className="min-h-36 my-6 flex flex-col items-center justify-center">
|
||||
<div className="flex h-[72px] w-[72px] items-center justify-center rounded-full bg-gray-600 dark:bg-white">
|
||||
<CheckIcon className="inline-block h-10 w-10 text-white dark:bg-white dark:text-gray-600" />
|
||||
</div>
|
||||
<div className="max-w-[420px] text-center">
|
||||
<h2 className="mt-6 mb-1 text-lg font-medium dark:text-gray-300">{t("all_done")}</h2>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const SetupFormStep1 = () => {
|
||||
const router = useRouter();
|
||||
const { t } = useLocale();
|
||||
|
||||
const formMethods = useForm<{
|
||||
username: string;
|
||||
email: string;
|
||||
fullname: string;
|
||||
password: string;
|
||||
}>({
|
||||
resolver: zodResolver(schema),
|
||||
});
|
||||
|
||||
return (
|
||||
<Form
|
||||
form={formMethods}
|
||||
id="setup-step-1"
|
||||
name="setup-step-1"
|
||||
className="space-y-4"
|
||||
handleSubmit={async (data) => {
|
||||
const response = await fetch("/api/auth/setup", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
username: data.username,
|
||||
fullname: data.fullname,
|
||||
email: data.email.toLowerCase(),
|
||||
password: data.password,
|
||||
}),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
if (response.status === 201) {
|
||||
router.replace("/auth/login");
|
||||
} else {
|
||||
router.replace(`/auth/setup`);
|
||||
}
|
||||
}}>
|
||||
<div>
|
||||
<label htmlFor="username" className="sr-only">
|
||||
{t("username")}
|
||||
</label>
|
||||
<div
|
||||
className={classNames(
|
||||
"mt-1 flex rounded-sm",
|
||||
formMethods.formState.errors.username ? "border-2 border-red-500" : ""
|
||||
)}>
|
||||
<span className="inline-flex items-center rounded-l-sm border border-r-0 border-gray-300 bg-gray-50 px-3 text-gray-500 sm:text-sm">
|
||||
cal.com/
|
||||
</span>
|
||||
|
||||
<Controller
|
||||
name="username"
|
||||
control={formMethods.control}
|
||||
defaultValue={router.query.username as string}
|
||||
render={({ field: { onBlur, onChange, value } }) => (
|
||||
<Input
|
||||
value={value || ""}
|
||||
onBlur={onBlur}
|
||||
onChange={async (e) => {
|
||||
onChange(e.target.value);
|
||||
formMethods.setValue("username", e.target.value);
|
||||
await formMethods.trigger("username");
|
||||
}}
|
||||
defaultValue={router.query.email}
|
||||
color={formMethods.formState.errors.username ? "warn" : ""}
|
||||
type="text"
|
||||
name="username"
|
||||
autoComplete="off"
|
||||
autoCorrect="off"
|
||||
autoCapitalize="none"
|
||||
placeholder={t("username")}
|
||||
className="rounded-r-s mt-0 block min-w-0 flex-1 rounded-none border-gray-300 px-3 py-2 sm:text-sm"
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
{formMethods.formState.errors.username && (
|
||||
<p role="alert" className="mt-1 text-xs text-red-500">
|
||||
{formMethods.formState.errors.username.message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div>
|
||||
<label htmlFor="fullname" className="sr-only">
|
||||
{t("full_name")}
|
||||
</label>
|
||||
<div
|
||||
className={classNames(
|
||||
"flex rounded-sm",
|
||||
formMethods.formState.errors.fullname ? "border-2 border-red-500" : "border-0"
|
||||
)}>
|
||||
<Controller
|
||||
name="fullname"
|
||||
control={formMethods.control}
|
||||
defaultValue={router.query.fullname as string}
|
||||
render={({ field: { onBlur, onChange, value } }) => (
|
||||
<Input
|
||||
value={value || ""}
|
||||
onBlur={onBlur}
|
||||
onChange={async (e) => {
|
||||
onChange(e.target.value);
|
||||
formMethods.setValue("fullname", e.target.value);
|
||||
await formMethods.trigger("fullname");
|
||||
}}
|
||||
defaultValue={router.query.fullname}
|
||||
color={formMethods.formState.errors.fullname ? "warn" : ""}
|
||||
type="text"
|
||||
name="fullname"
|
||||
autoCapitalize="none"
|
||||
autoComplete="name"
|
||||
autoCorrect="off"
|
||||
placeholder={t("full_name")}
|
||||
className={classNames(
|
||||
"rounded-r-s mt-0 block min-w-0 flex-1 rounded-none border-gray-300 px-3 py-2 sm:text-sm",
|
||||
formMethods.formState.errors.fullname
|
||||
? "border-r-0 focus:border-l focus:border-gray-300 focus:ring-0"
|
||||
: "focus:border-gray-900 focus:ring-gray-900"
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
{formMethods.formState.errors.fullname && (
|
||||
<p role="alert" className="mt-1 text-xs text-red-500">
|
||||
{formMethods.formState.errors.fullname.message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div>
|
||||
<label htmlFor="email" className="sr-only">
|
||||
{t("email_address")}
|
||||
</label>
|
||||
<div
|
||||
className={classNames(
|
||||
"flex rounded-sm",
|
||||
formMethods.formState.errors.email ? "border-2 border-red-500" : "border-0"
|
||||
)}>
|
||||
<Controller
|
||||
name="email"
|
||||
control={formMethods.control}
|
||||
defaultValue={router.query.email as string}
|
||||
render={({ field: { onBlur, onChange, value } }) => (
|
||||
<Input
|
||||
value={value || ""}
|
||||
onBlur={onBlur}
|
||||
onChange={async (e) => {
|
||||
onChange(e.target.value);
|
||||
formMethods.setValue("email", e.target.value);
|
||||
await formMethods.trigger("email");
|
||||
}}
|
||||
defaultValue={router.query.email}
|
||||
color={formMethods.formState.errors.email ? "warn" : ""}
|
||||
type="email"
|
||||
name="email"
|
||||
autoCapitalize="none"
|
||||
autoComplete="email"
|
||||
autoCorrect="off"
|
||||
placeholder={t("email_address")}
|
||||
className={classNames(
|
||||
"rounded-r-s mt-0 block min-w-0 flex-1 rounded-none border-gray-300 px-3 py-2 sm:text-sm",
|
||||
formMethods.formState.errors.email
|
||||
? "border-r-0 focus:border-l focus:border-gray-300 focus:ring-0"
|
||||
: "focus:border-gray-900 focus:ring-gray-900"
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
{formMethods.formState.errors.email && (
|
||||
<p role="alert" className="mt-1 text-xs text-red-500">
|
||||
{formMethods.formState.errors.email.message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div>
|
||||
<label htmlFor="password" className="sr-only">
|
||||
{t("password")}
|
||||
</label>
|
||||
<div
|
||||
className={classNames(
|
||||
"flex rounded-sm",
|
||||
formMethods.formState.errors.password ? "border-2 border-red-500" : "border-0"
|
||||
)}>
|
||||
<Controller
|
||||
name="password"
|
||||
control={formMethods.control}
|
||||
render={({ field: { onBlur, onChange, value } }) => (
|
||||
<Input
|
||||
value={value || ""}
|
||||
onBlur={onBlur}
|
||||
onChange={async (e) => {
|
||||
onChange(e.target.value);
|
||||
formMethods.setValue("password", e.target.value);
|
||||
await formMethods.trigger("password");
|
||||
}}
|
||||
color={formMethods.formState.errors.password ? "warn" : ""}
|
||||
type="password"
|
||||
name="password"
|
||||
autoComplete="off"
|
||||
placeholder={t("password")}
|
||||
className={classNames(
|
||||
"rounded-r-s mt-0 block min-w-0 flex-1 rounded-none border-gray-300 px-3 py-2 sm:text-sm",
|
||||
formMethods.formState.errors.password
|
||||
? "border-r-0 focus:border-l focus:border-gray-300 focus:ring-0"
|
||||
: "focus:border-gray-900 focus:ring-gray-900"
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
{formMethods.formState.errors.password && (
|
||||
<p role="alert" className="mt-1 max-w-xs text-xs text-red-500">
|
||||
{formMethods.formState.errors.password.message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<input type="submit" id="submit-setup-step-1" className="hidden" />
|
||||
</Form>
|
||||
);
|
||||
};
|
||||
|
||||
export default function Setup(props: inferSSRProps<typeof getServerSideProps>) {
|
||||
const { t } = useLocale();
|
||||
|
||||
const steps = [
|
||||
{
|
||||
title: t("administrator_user"),
|
||||
description: t("lets_create_first_administrator_user"),
|
||||
content: props.userCount !== 0 ? <StepDone /> : <SetupFormStep1 />,
|
||||
enabled: props.userCount === 0, // to check if the wizard should show buttons to navigate through more steps
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<>
|
||||
<main className="flex h-screen items-center bg-gray-100 print:h-full">
|
||||
<WizardForm href="/setup" steps={steps} />
|
||||
</main>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export const getServerSideProps = async () => {
|
||||
const userCount = await prisma.user.count();
|
||||
return {
|
||||
props: {
|
||||
userCount,
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -213,6 +213,7 @@
|
||||
"forgot_password": "Forgot Password",
|
||||
"forgot": "Forgot?",
|
||||
"done": "Done",
|
||||
"all_done": "All done!",
|
||||
"check_email_reset_password": "Check your email. We sent you a link to reset your password.",
|
||||
"finish": "Finish",
|
||||
"few_sentences_about_yourself": "A few sentences about yourself. This will appear on your personal url page.",
|
||||
@@ -477,6 +478,8 @@
|
||||
"member": "Member",
|
||||
"owner": "Owner",
|
||||
"admin": "Admin",
|
||||
"administrator_user": "Administrator user",
|
||||
"lets_create_first_administrator_user": "Let's create the first administrator user.",
|
||||
"new_member": "New Member",
|
||||
"invite": "Invite",
|
||||
"invite_new_member": "Invite a new member",
|
||||
|
||||
Reference in new issue
Block a user