Register.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <script setup lang="ts">
  2. import { useStore } from "vuex";
  3. import { defineAsyncComponent, ref, watch, onMounted } from "vue";
  4. import { useRoute } from "vue-router";
  5. import Toast from "toasters";
  6. import validation from "@/validation";
  7. const InputHelpBox = defineAsyncComponent(
  8. () => import("@/components/InputHelpBox.vue")
  9. );
  10. const route = useRoute();
  11. const username = ref({
  12. value: "",
  13. valid: false,
  14. entered: false,
  15. message: "Only letters, numbers and underscores are allowed."
  16. });
  17. const email = ref({
  18. value: "",
  19. valid: false,
  20. entered: false,
  21. message: "Please enter a valid email address."
  22. });
  23. const password = ref({
  24. value: "",
  25. valid: false,
  26. entered: false,
  27. visible: false,
  28. message:
  29. "Include at least one lowercase letter, one uppercase letter, one number and one special character."
  30. });
  31. const recaptcha = ref({
  32. key: "",
  33. token: "",
  34. enabled: false
  35. });
  36. const apiDomain = ref("");
  37. const siteSettings = ref({
  38. registrationDisabled: false,
  39. githubAuthentication: false
  40. });
  41. const passwordElement = ref();
  42. const store = useStore();
  43. const register = payload => store.dispatch("user/auth/register", payload);
  44. const openModal = payload =>
  45. store.dispatch("modalVisibility/openModal", payload);
  46. const closeCurrentModal = () =>
  47. store.dispatch("modalVisibility/closeCurrentModal");
  48. const submitModal = () => {
  49. if (!username.value.valid || !email.value.valid || !password.value.valid)
  50. return new Toast("Please ensure all fields are valid.");
  51. return register({
  52. username: username.value.value,
  53. email: email.value.value,
  54. password: password.value.value,
  55. recaptchaToken: recaptcha.value.token
  56. })
  57. .then(res => {
  58. if (res.status === "success") window.location.reload();
  59. })
  60. .catch(err => new Toast(err.message));
  61. };
  62. const togglePasswordVisibility = () => {
  63. if (passwordElement.value.type === "password") {
  64. passwordElement.value.type = "text";
  65. password.value.visible = true;
  66. } else {
  67. passwordElement.value.type = "password";
  68. password.value.visible = false;
  69. }
  70. };
  71. const changeToLoginModal = () => {
  72. closeCurrentModal();
  73. openModal("login");
  74. };
  75. const githubRedirect = () => {
  76. localStorage.setItem("github_redirect", route.path);
  77. };
  78. watch(
  79. () => username.value.value,
  80. value => {
  81. username.value.entered = true;
  82. if (!validation.isLength(value, 2, 32)) {
  83. username.value.message =
  84. "Username must have between 2 and 32 characters.";
  85. username.value.valid = false;
  86. } else if (!validation.regex.azAZ09_.test(value)) {
  87. username.value.message =
  88. "Invalid format. Allowed characters: a-z, A-Z, 0-9 and _.";
  89. username.value.valid = false;
  90. } else if (value.replaceAll(/[_]/g, "").length === 0) {
  91. username.value.message =
  92. "Invalid format. Allowed characters: a-z, A-Z, 0-9 and _, and there has to be at least one letter or number.";
  93. username.value.valid = false;
  94. } else {
  95. username.value.message = "Everything looks great!";
  96. username.value.valid = true;
  97. }
  98. }
  99. );
  100. watch(
  101. () => email.value.value,
  102. value => {
  103. email.value.entered = true;
  104. if (!validation.isLength(value, 3, 254)) {
  105. email.value.message =
  106. "Email must have between 3 and 254 characters.";
  107. email.value.valid = false;
  108. } else if (
  109. value.indexOf("@") !== value.lastIndexOf("@") ||
  110. !validation.regex.emailSimple.test(value)
  111. ) {
  112. email.value.message = "Invalid format.";
  113. email.value.valid = false;
  114. } else {
  115. email.value.message = "Everything looks great!";
  116. email.value.valid = true;
  117. }
  118. }
  119. );
  120. watch(
  121. () => password.value.value,
  122. value => {
  123. password.value.entered = true;
  124. if (!validation.isLength(value, 6, 200)) {
  125. password.value.message =
  126. "Password must have between 6 and 200 characters.";
  127. password.value.valid = false;
  128. } else if (!validation.regex.password.test(value)) {
  129. password.value.message =
  130. "Include at least one lowercase letter, one uppercase letter, one number and one special character.";
  131. password.value.valid = false;
  132. } else {
  133. password.value.message = "Everything looks great!";
  134. password.value.valid = true;
  135. }
  136. }
  137. );
  138. onMounted(async () => {
  139. apiDomain.value = await lofig.get("backend.apiDomain");
  140. lofig.get("siteSettings").then(settings => {
  141. if (settings.registrationDisabled) {
  142. new Toast("Registration is disabled.");
  143. closeCurrentModal();
  144. } else {
  145. siteSettings.value = settings;
  146. }
  147. });
  148. lofig.get("recaptcha").then(obj => {
  149. recaptcha.value.enabled = obj.enabled;
  150. if (obj.enabled === true) {
  151. recaptcha.value.key = obj.key;
  152. const recaptchaScript = document.createElement("script");
  153. recaptchaScript.onload = () => {
  154. grecaptcha.ready(() => {
  155. grecaptcha
  156. .execute(recaptcha.value.key, { action: "login" })
  157. .then(token => {
  158. recaptcha.value.token = token;
  159. });
  160. });
  161. };
  162. recaptchaScript.setAttribute(
  163. "src",
  164. `https://www.google.com/recaptcha/api.js?render=${recaptcha.value.key}`
  165. );
  166. document.head.appendChild(recaptchaScript);
  167. }
  168. });
  169. });
  170. </script>
  171. <template>
  172. <div>
  173. <modal
  174. title="Register"
  175. class="register-modal"
  176. :size="'slim'"
  177. @closed="closeCurrentModal()"
  178. >
  179. <template #body>
  180. <!-- email address -->
  181. <p class="control">
  182. <label class="label">Email</label>
  183. <input
  184. v-model="email.value"
  185. class="input"
  186. type="email"
  187. placeholder="Email..."
  188. @keyup.enter="submitModal()"
  189. autofocus
  190. />
  191. </p>
  192. <transition name="fadein-helpbox">
  193. <input-help-box
  194. :entered="email.entered"
  195. :valid="email.valid"
  196. :message="email.message"
  197. />
  198. </transition>
  199. <!-- username -->
  200. <p class="control">
  201. <label class="label">Username</label>
  202. <input
  203. v-model="username.value"
  204. class="input"
  205. type="text"
  206. placeholder="Username..."
  207. @keyup.enter="submitModal()"
  208. />
  209. </p>
  210. <transition name="fadein-helpbox">
  211. <input-help-box
  212. :entered="username.entered"
  213. :valid="username.valid"
  214. :message="username.message"
  215. />
  216. </transition>
  217. <!-- password -->
  218. <p class="control">
  219. <label class="label">Password</label>
  220. </p>
  221. <div id="password-visibility-container">
  222. <input
  223. v-model="password.value"
  224. class="input"
  225. type="password"
  226. ref="passwordElement"
  227. placeholder="Password..."
  228. @keyup.enter="submitModal()"
  229. />
  230. <a @click="togglePasswordVisibility()">
  231. <i class="material-icons">
  232. {{
  233. !password.visible
  234. ? "visibility"
  235. : "visibility_off"
  236. }}
  237. </i>
  238. </a>
  239. </div>
  240. <transition name="fadein-helpbox">
  241. <input-help-box
  242. :valid="password.valid"
  243. :entered="password.entered"
  244. :message="password.message"
  245. />
  246. </transition>
  247. <br />
  248. <p>
  249. By registering you agree to our
  250. <router-link to="/terms" @click="closeCurrentModal()">
  251. Terms of Service
  252. </router-link>
  253. and
  254. <router-link to="/privacy" @click="closeCurrentModal()">
  255. Privacy Policy</router-link
  256. >.
  257. </p>
  258. </template>
  259. <template #footer>
  260. <div id="actions">
  261. <button class="button is-primary" @click="submitModal()">
  262. Register
  263. </button>
  264. <a
  265. v-if="siteSettings.githubAuthentication"
  266. class="button is-github"
  267. :href="apiDomain + '/auth/github/authorize'"
  268. @click="githubRedirect()"
  269. >
  270. <div class="icon">
  271. <img
  272. class="invert"
  273. src="/assets/social/github.svg"
  274. />
  275. </div>
  276. &nbsp;&nbsp;Register with GitHub
  277. </a>
  278. </div>
  279. <p class="content-box-optional-helper">
  280. <a @click="changeToLoginModal()">
  281. Already have an account?
  282. </a>
  283. </p>
  284. </template>
  285. </modal>
  286. </div>
  287. </template>
  288. <style lang="less" scoped>
  289. .night-mode {
  290. .modal-card,
  291. .modal-card-head,
  292. .modal-card-body,
  293. .modal-card-foot {
  294. background-color: var(--dark-grey-3);
  295. }
  296. .label,
  297. p:not(.help) {
  298. color: var(--light-grey-2);
  299. }
  300. }
  301. .control {
  302. margin-bottom: 2px !important;
  303. }
  304. .modal-card-foot {
  305. display: flex;
  306. justify-content: space-between;
  307. flex-wrap: wrap;
  308. .content-box-optional-helper {
  309. margin-top: 0;
  310. }
  311. }
  312. .button.is-github {
  313. background-color: var(--dark-grey-2);
  314. color: var(--white) !important;
  315. }
  316. .is-github:focus {
  317. background-color: var(--dark-grey-4);
  318. }
  319. .invert {
  320. filter: brightness(5);
  321. }
  322. #recaptcha {
  323. padding: 10px 0;
  324. }
  325. a {
  326. color: var(--primary-color);
  327. }
  328. </style>
  329. <style lang="less">
  330. .grecaptcha-badge {
  331. z-index: 2000;
  332. }
  333. </style>