Register.vue 6.8 KB

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