Register.vue 8.5 KB

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