Register.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 validation from "@/validation";
  133. import InputHelpBox from "../InputHelpBox.vue";
  134. export default {
  135. components: { InputHelpBox },
  136. data() {
  137. return {
  138. username: {
  139. value: "",
  140. valid: false,
  141. entered: false,
  142. message: "Only letters, numbers and underscores are allowed."
  143. },
  144. email: {
  145. value: "",
  146. valid: false,
  147. entered: false,
  148. message: "Please enter a valid email address."
  149. },
  150. password: {
  151. value: "",
  152. valid: false,
  153. entered: false,
  154. visible: false,
  155. message:
  156. "Include at least one lowercase letter, one uppercase letter, one number and one special character."
  157. },
  158. recaptcha: {
  159. key: "",
  160. token: "",
  161. enabled: false
  162. },
  163. apiDomain: "",
  164. registrationDisabled: false
  165. };
  166. },
  167. watch: {
  168. // eslint-disable-next-line
  169. "username.value": function (value) {
  170. if (!validation.isLength(value, 2, 32)) {
  171. this.username.message =
  172. "Username must have between 2 and 32 characters.";
  173. this.username.valid = false;
  174. } else if (!validation.regex.azAZ09_.test(value)) {
  175. this.username.message =
  176. "Invalid format. Allowed characters: a-z, A-Z, 0-9 and _.";
  177. this.username.valid = false;
  178. } else if (value.replaceAll(/[_]/g, "").length === 0) {
  179. this.username.message =
  180. "Invalid format. Allowed characters: a-z, A-Z, 0-9 and _, and there has to be at least one letter or number.";
  181. this.username.valid = false;
  182. } else {
  183. this.username.message = "Everything looks great!";
  184. this.username.valid = true;
  185. }
  186. },
  187. // eslint-disable-next-line
  188. "email.value": function (value) {
  189. if (!validation.isLength(value, 3, 254)) {
  190. this.email.message =
  191. "Email must have between 3 and 254 characters.";
  192. this.email.valid = false;
  193. } else if (
  194. value.indexOf("@") !== value.lastIndexOf("@") ||
  195. !validation.regex.emailSimple.test(value)
  196. ) {
  197. this.email.message = "Invalid format.";
  198. this.email.valid = false;
  199. } else {
  200. this.email.message = "Everything looks great!";
  201. this.email.valid = true;
  202. }
  203. },
  204. // eslint-disable-next-line
  205. "password.value": function (value) {
  206. if (!validation.isLength(value, 6, 200)) {
  207. this.password.message =
  208. "Password must have between 6 and 200 characters.";
  209. this.password.valid = false;
  210. } else if (!validation.regex.password.test(value)) {
  211. this.password.message =
  212. "Include at least one lowercase letter, one uppercase letter, one number and one special character.";
  213. this.password.valid = false;
  214. } else {
  215. this.password.message = "Everything looks great!";
  216. this.password.valid = true;
  217. }
  218. }
  219. },
  220. async mounted() {
  221. this.apiDomain = await lofig.get("backend.apiDomain");
  222. lofig
  223. .get("siteSettings.registrationDisabled")
  224. .then(registrationDisabled => {
  225. this.registrationDisabled = registrationDisabled;
  226. if (registrationDisabled) {
  227. new Toast("Registration is disabled.");
  228. this.closeModal("register");
  229. }
  230. });
  231. lofig.get("recaptcha").then(obj => {
  232. this.recaptcha.enabled = obj.enabled;
  233. if (obj.enabled === true) {
  234. this.recaptcha.key = obj.key;
  235. const recaptchaScript = document.createElement("script");
  236. recaptchaScript.onload = () => {
  237. grecaptcha.ready(() => {
  238. grecaptcha
  239. .execute(this.recaptcha.key, { action: "login" })
  240. .then(token => {
  241. this.recaptcha.token = token;
  242. });
  243. });
  244. };
  245. recaptchaScript.setAttribute(
  246. "src",
  247. `https://www.google.com/recaptcha/api.js?render=${this.recaptcha.key}`
  248. );
  249. document.head.appendChild(recaptchaScript);
  250. }
  251. });
  252. },
  253. methods: {
  254. submitOnEnter: (cb, event) => {
  255. if (event.which === 13) cb();
  256. },
  257. togglePasswordVisibility() {
  258. if (this.$refs.password.type === "password") {
  259. this.$refs.password.type = "text";
  260. this.password.visible = true;
  261. } else {
  262. this.$refs.password.type = "password";
  263. this.password.visible = false;
  264. }
  265. },
  266. changeToLoginModal() {
  267. this.closeRegisterModal();
  268. this.openModal("login");
  269. },
  270. closeRegisterModal() {
  271. this.closeModal("register");
  272. },
  273. submitModal() {
  274. if (
  275. !this.username.valid ||
  276. !this.email.valid ||
  277. !this.password.valid
  278. )
  279. return new Toast("Please ensure all fields are valid.");
  280. return this.register({
  281. username: this.username.value,
  282. email: this.email.value,
  283. password: this.password.value,
  284. recaptchaToken: this.recaptcha.token
  285. })
  286. .then(res => {
  287. if (res.status === "success") window.location.reload();
  288. })
  289. .catch(err => new Toast(err.message));
  290. },
  291. onInput(inputName) {
  292. this[inputName].entered = true;
  293. },
  294. githubRedirect() {
  295. localStorage.setItem("github_redirect", this.$route.path);
  296. },
  297. ...mapActions("modalVisibility", ["closeModal", "openModal"]),
  298. ...mapActions("user/auth", ["register"])
  299. }
  300. };
  301. </script>
  302. <style lang="less" scoped>
  303. .night-mode {
  304. .modal-card,
  305. .modal-card-head,
  306. .modal-card-body,
  307. .modal-card-foot {
  308. background-color: var(--dark-grey-3);
  309. }
  310. .label,
  311. p:not(.help) {
  312. color: var(--light-grey-2);
  313. }
  314. }
  315. .control {
  316. margin-bottom: 2px !important;
  317. }
  318. .modal-card-foot {
  319. display: flex;
  320. justify-content: space-between;
  321. flex-wrap: wrap;
  322. .content-box-optional-helper {
  323. margin-top: 0;
  324. }
  325. }
  326. .button.is-github {
  327. background-color: var(--dark-grey-2);
  328. color: var(--white) !important;
  329. }
  330. .is-github:focus {
  331. background-color: var(--dark-grey-4);
  332. }
  333. .invert {
  334. filter: brightness(5);
  335. }
  336. #recaptcha {
  337. padding: 10px 0;
  338. }
  339. a {
  340. color: var(--primary-color);
  341. }
  342. </style>
  343. <style lang="less">
  344. .grecaptcha-badge {
  345. z-index: 2000;
  346. }
  347. </style>