login-B1q1DWPj.mjs 16.9 KB
import { _ as _sfc_main$1 } from './Badge-DxP8LLFm.mjs';
import { d as useRoute$1, a as useToast, u as useAppI18n, b as useAuth, _ as _sfc_main$9, n as navigateTo, x as resolvePostLoginPath } from './server.mjs';
import { _ as _sfc_main$2 } from './Card-DYSPsF5s.mjs';
import { _ as _sfc_main$3 } from './Form-9sZcpyMB.mjs';
import { _ as _sfc_main$4 } from './FormField-CaARTmzs.mjs';
import { _ as _sfc_main$5 } from './Input-QBf9hfFU.mjs';
import { _ as _sfc_main$6 } from './Alert-D2pk4r51.mjs';
import { defineComponent, ref, computed, reactive, mergeProps, withCtx, unref, createTextVNode, toDisplayString, createVNode, openBlock, createBlock, createCommentVNode, useSSRContext } from 'vue';
import { ssrRenderAttrs, ssrRenderComponent, ssrInterpolate, ssrRenderList } from 'vue/server-renderer';
import * as z from 'zod';
import '../nitro/nitro.mjs';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'node:fs';
import 'node:path';
import 'node:crypto';
import 'node:url';
import '@iconify/utils';
import 'consola';
import 'perfect-debounce';
import 'tailwindcss/colors';
import '../routes/renderer.mjs';
import 'vue-bundle-renderer/runtime';
import 'unhead/server';
import 'devalue';
import 'unhead/plugins';
import 'unhead/utils';

const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "login",
  __ssrInlineRender: true,
  setup(__props) {
    const route = useRoute$1();
    const toast = useToast();
    const { t } = useAppI18n();
    const { login } = useAuth();
    const isSubmitting = ref(false);
    const errorMessage = ref("");
    const buildSchema = () => z.object({
      username: z.string().min(1, t("login.validation.usernameRequired")),
      password: z.string().min(1, t("login.validation.passwordRequired"))
    });
    const formSchema = computed(buildSchema);
    const formState = reactive({
      username: "",
      password: ""
    });
    const demoAccounts = computed(() => [{
      username: "admin",
      password: "123456",
      label: t("login.demo.admin")
    }, {
      username: "operator",
      password: "123456",
      label: t("login.demo.operator")
    }]);
    function fillDemoAccount(username, password) {
      formState.username = username;
      formState.password = password;
      errorMessage.value = "";
    }
    function getRedirectPath(roles) {
      return resolvePostLoginPath(route.query.redirect, roles);
    }
    function resolveErrorMessage(errorCode, fallback) {
      if (errorCode === "VALIDATION_ERROR") {
        return t("login.errors.validation");
      }
      if (errorCode === "INVALID_CREDENTIALS") {
        return t("login.errors.invalidCredentials");
      }
      return fallback;
    }
    async function onSubmit(event) {
      isSubmitting.value = true;
      errorMessage.value = "";
      try {
        const result = await login(event.data);
        if (!result.success || !result.token || !result.user) {
          errorMessage.value = resolveErrorMessage(result.errorCode, result.message);
          return;
        }
        toast.add({
          title: t("login.toast.successTitle"),
          description: t("login.toast.successDescription", { name: result.user.name }),
          color: "success",
          icon: "i-lucide-check"
        });
        await navigateTo(getRedirectPath(result.user.roles));
      } catch {
        errorMessage.value = t("login.errors.requestFailed");
      } finally {
        isSubmitting.value = false;
      }
    }
    return (_ctx, _push, _parent, _attrs) => {
      const _component_UBadge = _sfc_main$1;
      const _component_UButton = _sfc_main$9;
      const _component_UCard = _sfc_main$2;
      const _component_UForm = _sfc_main$3;
      const _component_UFormField = _sfc_main$4;
      const _component_UInput = _sfc_main$5;
      const _component_UAlert = _sfc_main$6;
      _push(`<main${ssrRenderAttrs(mergeProps({ class: "min-h-screen bg-gradient-to-br from-default via-elevated/30 to-primary/10" }, _attrs))}><div class="mx-auto grid min-h-screen max-w-6xl items-center gap-8 px-4 py-10 lg:grid-cols-2 lg:px-8"><section class="space-y-5">`);
      _push(ssrRenderComponent(_component_UBadge, {
        color: "primary",
        variant: "soft",
        class: "rounded-full"
      }, {
        default: withCtx((_, _push2, _parent2, _scopeId) => {
          if (_push2) {
            _push2(`${ssrInterpolate(unref(t)("login.badge"))}`);
          } else {
            return [
              createTextVNode(toDisplayString(unref(t)("login.badge")), 1)
            ];
          }
        }),
        _: 1
      }, _parent));
      _push(`<h1 class="text-3xl font-semibold text-highlighted sm:text-4xl">${ssrInterpolate(unref(t)("login.title"))}</h1><p class="text-toned text-base leading-7">${ssrInterpolate(unref(t)("login.description"))}</p><div class="space-y-3"><p class="text-sm font-medium text-muted">${ssrInterpolate(unref(t)("login.demo.title"))}</p><div class="flex flex-wrap gap-2"><!--[-->`);
      ssrRenderList(unref(demoAccounts), (account) => {
        _push(ssrRenderComponent(_component_UButton, {
          key: account.username,
          color: "neutral",
          variant: "soft",
          size: "sm",
          onClick: ($event) => fillDemoAccount(account.username, account.password)
        }, {
          default: withCtx((_, _push2, _parent2, _scopeId) => {
            if (_push2) {
              _push2(`${ssrInterpolate(account.label)}`);
            } else {
              return [
                createTextVNode(toDisplayString(account.label), 1)
              ];
            }
          }),
          _: 2
        }, _parent));
      });
      _push(`<!--]--></div></div></section><section>`);
      _push(ssrRenderComponent(_component_UCard, { class: "w-full shadow-sm ring-1 ring-default" }, {
        header: withCtx((_, _push2, _parent2, _scopeId) => {
          if (_push2) {
            _push2(`<div class="space-y-1"${_scopeId}><h2 class="text-xl font-semibold text-highlighted"${_scopeId}>${ssrInterpolate(unref(t)("login.form.title"))}</h2><p class="text-sm text-muted"${_scopeId}>${ssrInterpolate(unref(t)("login.form.description"))}</p></div>`);
          } else {
            return [
              createVNode("div", { class: "space-y-1" }, [
                createVNode("h2", { class: "text-xl font-semibold text-highlighted" }, toDisplayString(unref(t)("login.form.title")), 1),
                createVNode("p", { class: "text-sm text-muted" }, toDisplayString(unref(t)("login.form.description")), 1)
              ])
            ];
          }
        }),
        default: withCtx((_, _push2, _parent2, _scopeId) => {
          if (_push2) {
            _push2(ssrRenderComponent(_component_UForm, {
              schema: unref(formSchema),
              state: unref(formState),
              class: "space-y-5",
              onSubmit
            }, {
              default: withCtx((_2, _push3, _parent3, _scopeId2) => {
                if (_push3) {
                  _push3(ssrRenderComponent(_component_UFormField, {
                    name: "username",
                    label: unref(t)("login.form.username"),
                    required: ""
                  }, {
                    default: withCtx((_3, _push4, _parent4, _scopeId3) => {
                      if (_push4) {
                        _push4(ssrRenderComponent(_component_UInput, {
                          modelValue: unref(formState).username,
                          "onUpdate:modelValue": ($event) => unref(formState).username = $event,
                          size: "xl",
                          class: "w-full",
                          placeholder: unref(t)("login.form.usernamePlaceholder"),
                          autocomplete: "username"
                        }, null, _parent4, _scopeId3));
                      } else {
                        return [
                          createVNode(_component_UInput, {
                            modelValue: unref(formState).username,
                            "onUpdate:modelValue": ($event) => unref(formState).username = $event,
                            size: "xl",
                            class: "w-full",
                            placeholder: unref(t)("login.form.usernamePlaceholder"),
                            autocomplete: "username"
                          }, null, 8, ["modelValue", "onUpdate:modelValue", "placeholder"])
                        ];
                      }
                    }),
                    _: 1
                  }, _parent3, _scopeId2));
                  _push3(ssrRenderComponent(_component_UFormField, {
                    name: "password",
                    label: unref(t)("login.form.password"),
                    required: ""
                  }, {
                    default: withCtx((_3, _push4, _parent4, _scopeId3) => {
                      if (_push4) {
                        _push4(ssrRenderComponent(_component_UInput, {
                          modelValue: unref(formState).password,
                          "onUpdate:modelValue": ($event) => unref(formState).password = $event,
                          size: "xl",
                          class: "w-full",
                          placeholder: unref(t)("login.form.passwordPlaceholder"),
                          type: "password",
                          autocomplete: "current-password"
                        }, null, _parent4, _scopeId3));
                      } else {
                        return [
                          createVNode(_component_UInput, {
                            modelValue: unref(formState).password,
                            "onUpdate:modelValue": ($event) => unref(formState).password = $event,
                            size: "xl",
                            class: "w-full",
                            placeholder: unref(t)("login.form.passwordPlaceholder"),
                            type: "password",
                            autocomplete: "current-password"
                          }, null, 8, ["modelValue", "onUpdate:modelValue", "placeholder"])
                        ];
                      }
                    }),
                    _: 1
                  }, _parent3, _scopeId2));
                  if (unref(errorMessage)) {
                    _push3(ssrRenderComponent(_component_UAlert, {
                      color: "error",
                      variant: "soft",
                      icon: "i-lucide-circle-alert",
                      title: unref(t)("login.errors.title"),
                      description: unref(errorMessage)
                    }, null, _parent3, _scopeId2));
                  } else {
                    _push3(`<!---->`);
                  }
                  _push3(ssrRenderComponent(_component_UButton, {
                    type: "submit",
                    color: "primary",
                    block: "",
                    size: "xl",
                    loading: unref(isSubmitting),
                    label: unref(t)("login.form.submit")
                  }, null, _parent3, _scopeId2));
                } else {
                  return [
                    createVNode(_component_UFormField, {
                      name: "username",
                      label: unref(t)("login.form.username"),
                      required: ""
                    }, {
                      default: withCtx(() => [
                        createVNode(_component_UInput, {
                          modelValue: unref(formState).username,
                          "onUpdate:modelValue": ($event) => unref(formState).username = $event,
                          size: "xl",
                          class: "w-full",
                          placeholder: unref(t)("login.form.usernamePlaceholder"),
                          autocomplete: "username"
                        }, null, 8, ["modelValue", "onUpdate:modelValue", "placeholder"])
                      ]),
                      _: 1
                    }, 8, ["label"]),
                    createVNode(_component_UFormField, {
                      name: "password",
                      label: unref(t)("login.form.password"),
                      required: ""
                    }, {
                      default: withCtx(() => [
                        createVNode(_component_UInput, {
                          modelValue: unref(formState).password,
                          "onUpdate:modelValue": ($event) => unref(formState).password = $event,
                          size: "xl",
                          class: "w-full",
                          placeholder: unref(t)("login.form.passwordPlaceholder"),
                          type: "password",
                          autocomplete: "current-password"
                        }, null, 8, ["modelValue", "onUpdate:modelValue", "placeholder"])
                      ]),
                      _: 1
                    }, 8, ["label"]),
                    unref(errorMessage) ? (openBlock(), createBlock(_component_UAlert, {
                      key: 0,
                      color: "error",
                      variant: "soft",
                      icon: "i-lucide-circle-alert",
                      title: unref(t)("login.errors.title"),
                      description: unref(errorMessage)
                    }, null, 8, ["title", "description"])) : createCommentVNode("", true),
                    createVNode(_component_UButton, {
                      type: "submit",
                      color: "primary",
                      block: "",
                      size: "xl",
                      loading: unref(isSubmitting),
                      label: unref(t)("login.form.submit")
                    }, null, 8, ["loading", "label"])
                  ];
                }
              }),
              _: 1
            }, _parent2, _scopeId));
          } else {
            return [
              createVNode(_component_UForm, {
                schema: unref(formSchema),
                state: unref(formState),
                class: "space-y-5",
                onSubmit
              }, {
                default: withCtx(() => [
                  createVNode(_component_UFormField, {
                    name: "username",
                    label: unref(t)("login.form.username"),
                    required: ""
                  }, {
                    default: withCtx(() => [
                      createVNode(_component_UInput, {
                        modelValue: unref(formState).username,
                        "onUpdate:modelValue": ($event) => unref(formState).username = $event,
                        size: "xl",
                        class: "w-full",
                        placeholder: unref(t)("login.form.usernamePlaceholder"),
                        autocomplete: "username"
                      }, null, 8, ["modelValue", "onUpdate:modelValue", "placeholder"])
                    ]),
                    _: 1
                  }, 8, ["label"]),
                  createVNode(_component_UFormField, {
                    name: "password",
                    label: unref(t)("login.form.password"),
                    required: ""
                  }, {
                    default: withCtx(() => [
                      createVNode(_component_UInput, {
                        modelValue: unref(formState).password,
                        "onUpdate:modelValue": ($event) => unref(formState).password = $event,
                        size: "xl",
                        class: "w-full",
                        placeholder: unref(t)("login.form.passwordPlaceholder"),
                        type: "password",
                        autocomplete: "current-password"
                      }, null, 8, ["modelValue", "onUpdate:modelValue", "placeholder"])
                    ]),
                    _: 1
                  }, 8, ["label"]),
                  unref(errorMessage) ? (openBlock(), createBlock(_component_UAlert, {
                    key: 0,
                    color: "error",
                    variant: "soft",
                    icon: "i-lucide-circle-alert",
                    title: unref(t)("login.errors.title"),
                    description: unref(errorMessage)
                  }, null, 8, ["title", "description"])) : createCommentVNode("", true),
                  createVNode(_component_UButton, {
                    type: "submit",
                    color: "primary",
                    block: "",
                    size: "xl",
                    loading: unref(isSubmitting),
                    label: unref(t)("login.form.submit")
                  }, null, 8, ["loading", "label"])
                ]),
                _: 1
              }, 8, ["schema", "state"])
            ];
          }
        }),
        _: 1
      }, _parent));
      _push(`</section></div></main>`);
    };
  }
});
const _sfc_setup = _sfc_main.setup;
_sfc_main.setup = (props, ctx) => {
  const ssrContext = useSSRContext();
  (ssrContext.modules || (ssrContext.modules = /* @__PURE__ */ new Set())).add("pages/login.vue");
  return _sfc_setup ? _sfc_setup(props, ctx) : void 0;
};

export { _sfc_main as default };
//# sourceMappingURL=login-B1q1DWPj.mjs.map