123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- <script setup lang="ts">
- import { defineAsyncComponent, ref, watch, onMounted } from "vue";
- import { useRoute } from "vue-router";
- import Toast from "toasters";
- import { useConfigStore } from "@/stores/config";
- import { useUserAuthStore } from "@/stores/userAuth";
- import { useModalsStore } from "@/stores/modals";
- import validation from "@/validation";
- const Modal = defineAsyncComponent(() => import("@/components/Modal.vue"));
- const InputHelpBox = defineAsyncComponent(
- () => import("@/components/InputHelpBox.vue")
- );
- const route = useRoute();
- const username = ref({
- value: "",
- valid: false,
- entered: false,
- message: "Only letters, numbers and underscores are allowed."
- });
- const email = ref({
- value: "",
- valid: false,
- entered: false,
- message: "Please enter a valid email address."
- });
- const password = ref({
- value: "",
- valid: false,
- entered: false,
- visible: false,
- message:
- "Include at least one lowercase letter, one uppercase letter, one number and one special character."
- });
- const recaptchaToken = ref("");
- const passwordElement = ref();
- const { register } = useUserAuthStore();
- const configStore = useConfigStore();
- const { openModal, closeCurrentModal } = useModalsStore();
- const submitModal = () => {
- if (!username.value.valid || !email.value.valid || !password.value.valid)
- return new Toast("Please ensure all fields are valid.");
- return register({
- username: username.value.value,
- email: email.value.value,
- password: password.value.value,
- recaptchaToken: recaptchaToken.value
- })
- .then((res: any) => {
- if (res.status === "success") window.location.reload();
- })
- .catch(err => new Toast(err.message));
- };
- const togglePasswordVisibility = () => {
- if (passwordElement.value.type === "password") {
- passwordElement.value.type = "text";
- password.value.visible = true;
- } else {
- passwordElement.value.type = "password";
- password.value.visible = false;
- }
- };
- const changeToLoginModal = () => {
- closeCurrentModal();
- openModal("login");
- };
- const githubRedirect = () => {
- localStorage.setItem("github_redirect", route.path);
- };
- watch(
- () => username.value.value,
- value => {
- username.value.entered = true;
- if (!validation.isLength(value, 2, 32)) {
- username.value.message =
- "Username must have between 2 and 32 characters.";
- username.value.valid = false;
- } else if (!validation.regex.azAZ09_.test(value)) {
- username.value.message =
- "Invalid format. Allowed characters: a-z, A-Z, 0-9 and _.";
- username.value.valid = false;
- } else if (value.replaceAll(/[_]/g, "").length === 0) {
- username.value.message =
- "Invalid format. Allowed characters: a-z, A-Z, 0-9 and _, and there has to be at least one letter or number.";
- username.value.valid = false;
- } else {
- username.value.message = "Everything looks great!";
- username.value.valid = true;
- }
- }
- );
- watch(
- () => email.value.value,
- value => {
- email.value.entered = true;
- if (!validation.isLength(value, 3, 254)) {
- email.value.message =
- "Email must have between 3 and 254 characters.";
- email.value.valid = false;
- } else if (
- value.indexOf("@") !== value.lastIndexOf("@") ||
- !validation.regex.emailSimple.test(value)
- ) {
- email.value.message = "Invalid format.";
- email.value.valid = false;
- } else {
- email.value.message = "Everything looks great!";
- email.value.valid = true;
- }
- }
- );
- watch(
- () => password.value.value,
- value => {
- password.value.entered = true;
- if (!validation.isLength(value, 6, 200)) {
- password.value.message =
- "Password must have between 6 and 200 characters.";
- password.value.valid = false;
- } else if (!validation.regex.password.test(value)) {
- password.value.message =
- "Include at least one lowercase letter, one uppercase letter, one number and one special character.";
- password.value.valid = false;
- } else {
- password.value.message = "Everything looks great!";
- password.value.valid = true;
- }
- }
- );
- onMounted(async () => {
- if (configStore.get("registrationDisabled")) {
- new Toast("Registration is disabled.");
- closeCurrentModal();
- }
- if (configStore.get("recaptcha.enabled")) {
- const recaptchaScript = document.createElement("script");
- recaptchaScript.onload = () => {
- grecaptcha.ready(() => {
- grecaptcha
- .execute(configStore.get("recaptcha.key"), {
- action: "login"
- })
- .then(token => {
- recaptchaToken.value = token;
- });
- });
- };
- recaptchaScript.setAttribute(
- "src",
- `https://www.google.com/recaptcha/api.js?render=${configStore.get(
- "recaptcha.key"
- )}`
- );
- document.head.appendChild(recaptchaScript);
- }
- });
- </script>
- <template>
- <div>
- <modal
- title="Register"
- class="register-modal"
- :size="'slim'"
- @closed="closeCurrentModal()"
- >
- <template #body>
- <!-- email address -->
- <p class="control">
- <label class="label">Email</label>
- <input
- v-model="email.value"
- class="input"
- type="email"
- placeholder="Email..."
- @keyup.enter="submitModal()"
- autofocus
- />
- </p>
- <transition name="fadein-helpbox">
- <input-help-box
- :entered="email.entered"
- :valid="email.valid"
- :message="email.message"
- />
- </transition>
- <!-- username -->
- <p class="control">
- <label class="label">Username</label>
- <input
- v-model="username.value"
- class="input"
- type="text"
- placeholder="Username..."
- @keyup.enter="submitModal()"
- />
- </p>
- <transition name="fadein-helpbox">
- <input-help-box
- :entered="username.entered"
- :valid="username.valid"
- :message="username.message"
- />
- </transition>
- <!-- password -->
- <p class="control">
- <label class="label">Password</label>
- </p>
- <div id="password-visibility-container">
- <input
- v-model="password.value"
- class="input"
- type="password"
- ref="passwordElement"
- placeholder="Password..."
- @keyup.enter="submitModal()"
- />
- <a @click="togglePasswordVisibility()">
- <i class="material-icons">
- {{
- !password.visible
- ? "visibility"
- : "visibility_off"
- }}
- </i>
- </a>
- </div>
- <transition name="fadein-helpbox">
- <input-help-box
- :valid="password.valid"
- :entered="password.entered"
- :message="password.message"
- />
- </transition>
- <br />
- <p>
- By registering you agree to our
- <router-link to="/terms" @click="closeCurrentModal()">
- Terms of Service
- </router-link>
- and
- <router-link to="/privacy" @click="closeCurrentModal()">
- Privacy Policy</router-link
- >.
- </p>
- </template>
- <template #footer>
- <div id="actions">
- <button class="button is-primary" @click="submitModal()">
- Register
- </button>
- <a
- v-if="configStore.get('githubAuthentication')"
- class="button is-github"
- :href="configStore.urls.api + '/auth/github/authorize'"
- @click="githubRedirect()"
- >
- <div class="icon">
- <img
- class="invert"
- src="/assets/social/github.svg"
- />
- </div>
- Register with GitHub
- </a>
- </div>
- <p class="content-box-optional-helper">
- <a @click="changeToLoginModal()">
- Already have an account?
- </a>
- </p>
- </template>
- </modal>
- </div>
- </template>
- <style lang="less" scoped>
- .night-mode {
- .modal-card,
- .modal-card-head,
- .modal-card-body,
- .modal-card-foot {
- background-color: var(--dark-grey-3);
- }
- .label,
- p:not(.help) {
- color: var(--light-grey-2);
- }
- }
- .control {
- margin-bottom: 2px !important;
- }
- .modal-card-foot {
- display: flex;
- justify-content: space-between;
- flex-wrap: wrap;
- .content-box-optional-helper {
- margin-top: 0;
- }
- }
- .button.is-github {
- background-color: var(--dark-grey-2);
- color: var(--white) !important;
- }
- .is-github:focus {
- background-color: var(--dark-grey-4);
- }
- .invert {
- filter: brightness(5);
- }
- #recaptcha {
- padding: 10px 0;
- }
- a {
- color: var(--primary-color);
- }
- </style>
- <style lang="less">
- .grecaptcha-badge {
- z-index: 2000;
- }
- </style>
|