Register.vue 8.6 KB

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