Register.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <template>
  2. <div class="modal is-active">
  3. <div
  4. class="modal-background"
  5. @click="
  6. closeModal({
  7. sector: 'header',
  8. modal: 'register'
  9. })
  10. "
  11. />
  12. <div class="modal-card">
  13. <header class="modal-card-head">
  14. <p class="modal-card-title">
  15. Register
  16. </p>
  17. <button
  18. class="delete"
  19. @click="
  20. closeModal({
  21. sector: 'header',
  22. modal: 'register'
  23. })
  24. "
  25. />
  26. </header>
  27. <section class="modal-card-body">
  28. <!-- validation to check if exists http://bulma.io/documentation/elements/form/ -->
  29. <label class="label">Email</label>
  30. <p class="control">
  31. <input
  32. v-model="email.value"
  33. class="input"
  34. type="email"
  35. placeholder="Email..."
  36. autofocus
  37. />
  38. </p>
  39. <p
  40. class="help"
  41. :class="email.valid ? 'is-success' : 'is-danger'"
  42. >
  43. {{ email.message }}
  44. </p>
  45. <br />
  46. <label class="label">Username</label>
  47. <p class="control">
  48. <input
  49. v-model="username.value"
  50. class="input"
  51. type="text"
  52. placeholder="Username..."
  53. />
  54. </p>
  55. <p
  56. class="help"
  57. :class="username.valid ? 'is-success' : 'is-danger'"
  58. >
  59. {{ username.message }}
  60. </p>
  61. <br />
  62. <label class="label">Password</label>
  63. <p class="control">
  64. <input
  65. v-model="password.value"
  66. class="input"
  67. type="password"
  68. placeholder="Password..."
  69. @keypress="$parent.submitOnEnter(submitModal, $event)"
  70. />
  71. </p>
  72. <p
  73. class="help"
  74. :class="password.valid ? 'is-success' : 'is-danger'"
  75. >
  76. {{ password.message }}
  77. </p>
  78. <br />
  79. <p>
  80. By logging in/registering you agree to our
  81. <router-link to="/terms"> Terms of Service </router-link
  82. >&nbsp;and
  83. <router-link to="/privacy"> Privacy Policy </router-link>.
  84. </p>
  85. </section>
  86. <footer class="modal-card-foot">
  87. <a class="button is-primary" href="#" @click="submitModal()"
  88. >Submit</a
  89. >
  90. <a
  91. class="button is-github"
  92. :href="serverDomain + '/auth/github/authorize'"
  93. @click="githubRedirect()"
  94. >
  95. <div class="icon">
  96. <img class="invert" src="/assets/social/github.svg" />
  97. </div>
  98. &nbsp;&nbsp;Register with GitHub
  99. </a>
  100. </footer>
  101. </div>
  102. </div>
  103. </template>
  104. <script>
  105. import { mapActions } from "vuex";
  106. import Toast from "toasters";
  107. import validation from "../../validation";
  108. export default {
  109. data() {
  110. return {
  111. username: {
  112. value: "",
  113. valid: false,
  114. message: "Please enter a valid username."
  115. },
  116. email: {
  117. value: "",
  118. valid: false,
  119. message: "Please enter a valid email address."
  120. },
  121. password: {
  122. value: "",
  123. valid: false,
  124. message: "Please enter a valid password."
  125. },
  126. recaptcha: {
  127. key: "",
  128. token: ""
  129. },
  130. serverDomain: ""
  131. };
  132. },
  133. watch: {
  134. // eslint-disable-next-line func-names
  135. "username.value": function(value) {
  136. if (!validation.isLength(value, 2, 32)) {
  137. this.username.message =
  138. "Username must have between 2 and 32 characters.";
  139. this.username.valid = false;
  140. } else if (!validation.regex.azAZ09_.test(value)) {
  141. this.username.message =
  142. "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _.";
  143. this.username.valid = false;
  144. } else {
  145. this.username.message = "Everything looks great!";
  146. this.username.valid = true;
  147. }
  148. },
  149. // eslint-disable-next-line func-names
  150. "email.value": function(value) {
  151. if (!validation.isLength(value, 3, 254)) {
  152. this.email.message =
  153. "Email must have between 3 and 254 characters.";
  154. this.email.valid = false;
  155. } else if (
  156. value.indexOf("@") !== value.lastIndexOf("@") ||
  157. !validation.regex.emailSimple.test(value)
  158. ) {
  159. this.email.message = "Invalid Email format.";
  160. this.email.valid = false;
  161. } else {
  162. this.email.message = "Everything looks great!";
  163. this.email.valid = true;
  164. }
  165. },
  166. // eslint-disable-next-line func-names
  167. "password.value": function(value) {
  168. if (!validation.isLength(value, 6, 200)) {
  169. this.password.message =
  170. "Password must have between 6 and 200 characters.";
  171. this.password.valid = false;
  172. } else if (!validation.regex.password.test(value)) {
  173. this.password.message =
  174. "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.";
  175. this.password.valid = false;
  176. } else {
  177. this.password.message = "Everything looks great!";
  178. this.password.valid = true;
  179. }
  180. }
  181. },
  182. mounted() {
  183. lofig.get("serverDomain").then(serverDomain => {
  184. this.serverDomain = serverDomain;
  185. });
  186. lofig.get("recaptcha").then(obj => {
  187. this.recaptcha.key = obj.key;
  188. const recaptchaScript = document.createElement("script");
  189. recaptchaScript.onload = () => {
  190. grecaptcha.ready(() => {
  191. grecaptcha
  192. .execute(this.recaptcha.key, { action: "login" })
  193. .then(token => {
  194. this.recaptcha.token = token;
  195. });
  196. });
  197. };
  198. recaptchaScript.setAttribute(
  199. "src",
  200. `https://www.google.com/recaptcha/api.js?render=${this.recaptcha.key}`
  201. );
  202. document.head.appendChild(recaptchaScript);
  203. });
  204. },
  205. methods: {
  206. submitModal() {
  207. if (
  208. !this.username.valid ||
  209. !this.email.valid ||
  210. !this.password.valid
  211. )
  212. return new Toast({
  213. content: "Please ensure all fields are valid.",
  214. timeout: 5000
  215. });
  216. return this.register({
  217. username: this.username.value,
  218. email: this.email.value,
  219. password: this.password.value,
  220. recaptchaToken: this.recaptcha.token
  221. })
  222. .then(res => {
  223. if (res.status === "success") window.location.reload();
  224. })
  225. .catch(
  226. err => new Toast({ content: err.message, timeout: 5000 })
  227. );
  228. },
  229. githubRedirect() {
  230. localStorage.setItem("github_redirect", this.$route.path);
  231. },
  232. ...mapActions("modals", ["closeModal"]),
  233. ...mapActions("user/auth", ["register"])
  234. }
  235. };
  236. </script>
  237. <style lang="scss" scoped>
  238. @import "styles/global.scss";
  239. .night-mode {
  240. .modal-card,
  241. .modal-card-head,
  242. .modal-card-body,
  243. .modal-card-foot {
  244. background-color: $night-mode-secondary;
  245. }
  246. .label,
  247. p {
  248. color: #ddd;
  249. }
  250. }
  251. .button.is-github {
  252. background-color: $dark-grey-2;
  253. color: $white !important;
  254. }
  255. .is-github:focus {
  256. background-color: $dark-grey-3;
  257. }
  258. .is-primary:focus {
  259. background-color: #028bca !important;
  260. }
  261. .invert {
  262. filter: brightness(5);
  263. }
  264. #recaptcha {
  265. padding: 10px 0;
  266. }
  267. a {
  268. color: $primary-color;
  269. }
  270. </style>
  271. <style lang="scss">
  272. @import "styles/global.scss";
  273. .grecaptcha-badge {
  274. z-index: 2000;
  275. }
  276. </style>