Register.vue 8.6 KB

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