| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- import { useQueryClient } from "@tanstack/react-query";
- import cn from "classnames";
- import { Field, Form, Formik } from "formik";
- import { useState } from "react";
- import { Alert } from "react-bootstrap";
- import { createUser } from "src/api/backend";
- import { Button, LocalePicker, Page, ThemeSwitcher } from "src/components";
- import { useAuthState } from "src/context";
- import { intl } from "src/locale";
- import { validateEmail, validateString } from "src/modules/Validations";
- import styles from "./index.module.css";
- interface Payload {
- name: string;
- email: string;
- password: string;
- }
- export default function Setup() {
- const queryClient = useQueryClient();
- const { login } = useAuthState();
- const [errorMsg, setErrorMsg] = useState<string | null>(null);
- const onSubmit = async (values: Payload, { setSubmitting }: any) => {
- setErrorMsg(null);
- // Set a nickname, which is the first word of the name
- const nickname = values.name.split(" ")[0];
- const { password, ...payload } = {
- ...values,
- ...{
- nickname,
- auth: {
- type: "password",
- secret: values.password,
- },
- },
- };
- try {
- const user = await createUser(payload, true);
- if (user && typeof user.id !== "undefined" && user.id) {
- try {
- await login(user.email, password);
- // Trigger a Health change
- await queryClient.refetchQueries({ queryKey: ["health"] });
- // window.location.reload();
- } catch (err: any) {
- setErrorMsg(err.message);
- }
- } else {
- setErrorMsg("cannot_create_user");
- }
- } catch (err: any) {
- setErrorMsg(err.message);
- }
- setSubmitting(false);
- };
- return (
- <Page className="page page-center">
- <div className={cn("d-none", "d-md-flex", styles.helperBtns)}>
- <LocalePicker />
- <ThemeSwitcher />
- </div>
- <div className="container container-tight py-4">
- <div className="text-center mb-4">
- <img
- className={styles.logo}
- src="/images/logo-text-horizontal-grey.png"
- alt="Nginx Proxy Manager"
- />
- </div>
- <div className="card card-md">
- <Alert variant="danger" show={!!errorMsg} onClose={() => setErrorMsg(null)} dismissible>
- {errorMsg}
- </Alert>
- <Formik
- initialValues={
- {
- name: "",
- email: "",
- password: "",
- } as any
- }
- onSubmit={onSubmit}
- >
- {({ isSubmitting }) => (
- <Form>
- <div className="card-body text-center py-4 p-sm-5">
- <h1 className="mt-5">{intl.formatMessage({ id: "setup.title" })}</h1>
- <p className="text-secondary">{intl.formatMessage({ id: "setup.preamble" })}</p>
- </div>
- <hr />
- <div className="card-body">
- <div className="mb-3">
- <Field name="name" validate={validateString(1, 50)}>
- {({ field, form }: any) => (
- <div className="form-floating mb-3">
- <input
- id="name"
- className={`form-control ${form.errors.name && form.touched.name ? "is-invalid" : ""}`}
- placeholder={intl.formatMessage({ id: "user.full-name" })}
- {...field}
- />
- <label htmlFor="name">
- {intl.formatMessage({ id: "user.full-name" })}
- </label>
- {form.errors.name ? (
- <div className="invalid-feedback">
- {form.errors.name && form.touched.name
- ? form.errors.name
- : null}
- </div>
- ) : null}
- </div>
- )}
- </Field>
- </div>
- <div className="mb-3">
- <Field name="email" validate={validateEmail()}>
- {({ field, form }: any) => (
- <div className="form-floating mb-3">
- <input
- id="email"
- type="email"
- className={`form-control ${form.errors.email && form.touched.email ? "is-invalid" : ""}`}
- placeholder={intl.formatMessage({ id: "email-address" })}
- {...field}
- />
- <label htmlFor="email">
- {intl.formatMessage({ id: "email-address" })}
- </label>
- {form.errors.email ? (
- <div className="invalid-feedback">
- {form.errors.email && form.touched.email
- ? form.errors.email
- : null}
- </div>
- ) : null}
- </div>
- )}
- </Field>
- </div>
- <div className="mb-3">
- <Field name="password" validate={validateString(8, 100)}>
- {({ field, form }: any) => (
- <div className="form-floating mb-3">
- <input
- id="password"
- type="password"
- className={`form-control ${form.errors.password && form.touched.password ? "is-invalid" : ""}`}
- placeholder={intl.formatMessage({ id: "user.new-password" })}
- {...field}
- />
- <label htmlFor="password">
- {intl.formatMessage({ id: "user.new-password" })}
- </label>
- {form.errors.password ? (
- <div className="invalid-feedback">
- {form.errors.password && form.touched.password
- ? form.errors.password
- : null}
- </div>
- ) : null}
- </div>
- )}
- </Field>
- </div>
- </div>
- <div className="text-center my-3 mx-3">
- <Button
- type="submit"
- actionType="primary"
- data-bs-dismiss="modal"
- isLoading={isSubmitting}
- disabled={isSubmitting}
- className="w-100"
- >
- {intl.formatMessage({ id: "save" })}
- </Button>
- </div>
- </Form>
- )}
- </Formik>
- </div>
- </div>
- </Page>
- );
- }
|