Register.vue 8.6 KB

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