Register.vue 8.7 KB

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