Register.vue 8.5 KB

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