Login.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <script setup lang="ts">
  2. import { defineAsyncComponent, ref } from "vue";
  3. import { useRoute } from "vue-router";
  4. import Toast from "toasters";
  5. import { storeToRefs } from "pinia";
  6. import { useConfigStore } from "@/stores/config";
  7. import { useUserAuthStore } from "@/stores/userAuth";
  8. import { useModalsStore } from "@/stores/modals";
  9. const Modal = defineAsyncComponent(() => import("@/components/Modal.vue"));
  10. const route = useRoute();
  11. const email = ref("");
  12. const password = ref({
  13. value: "",
  14. visible: false
  15. });
  16. const passwordElement = ref();
  17. const configStore = useConfigStore();
  18. const { githubAuthentication, registrationDisabled } = storeToRefs(configStore);
  19. const { login } = useUserAuthStore();
  20. const { openModal, closeCurrentModal } = useModalsStore();
  21. const submitModal = () => {
  22. if (!email.value || !password.value.value) return;
  23. login({
  24. email: email.value,
  25. password: password.value.value
  26. })
  27. .then((res: any) => {
  28. if (res.status === "success") window.location.reload();
  29. })
  30. .catch(err => new Toast(err.message));
  31. };
  32. const checkForAutofill = (type, event) => {
  33. if (
  34. event.target.value !== "" &&
  35. event.inputType === undefined &&
  36. event.data === undefined &&
  37. event.dataTransfer === undefined &&
  38. event.isComposing === undefined
  39. )
  40. submitModal();
  41. };
  42. const togglePasswordVisibility = () => {
  43. if (passwordElement.value.type === "password") {
  44. passwordElement.value.type = "text";
  45. password.value.visible = true;
  46. } else {
  47. passwordElement.value.type = "password";
  48. password.value.visible = false;
  49. }
  50. };
  51. const changeToRegisterModal = () => {
  52. closeCurrentModal();
  53. openModal("register");
  54. };
  55. const githubRedirect = () => {
  56. localStorage.setItem("github_redirect", route.path);
  57. };
  58. </script>
  59. <template>
  60. <div>
  61. <modal
  62. title="Login"
  63. class="login-modal"
  64. :size="'slim'"
  65. @closed="closeCurrentModal()"
  66. >
  67. <template #body>
  68. <form>
  69. <!-- email address -->
  70. <p class="control">
  71. <label class="label">Username/Email</label>
  72. <input
  73. v-model="email"
  74. class="input"
  75. type="email"
  76. autocomplete="username"
  77. placeholder="Username/Email..."
  78. @input="checkForAutofill('email', $event)"
  79. @keyup.enter="submitModal()"
  80. />
  81. </p>
  82. <!-- password -->
  83. <p class="control">
  84. <label class="label">Password</label>
  85. </p>
  86. <div id="password-visibility-container">
  87. <input
  88. v-model="password.value"
  89. class="input"
  90. type="password"
  91. autocomplete="current-password"
  92. ref="passwordElement"
  93. placeholder="Password..."
  94. @input="checkForAutofill('password', $event)"
  95. @keyup.enter="submitModal()"
  96. />
  97. <a @click="togglePasswordVisibility()">
  98. <i class="material-icons">
  99. {{
  100. !password.visible
  101. ? "visibility"
  102. : "visibility_off"
  103. }}
  104. </i>
  105. </a>
  106. </div>
  107. <p
  108. v-if="configStore.mailEnabled"
  109. class="content-box-optional-helper"
  110. >
  111. <router-link
  112. id="forgot-password"
  113. to="/reset_password"
  114. @click="closeCurrentModal()"
  115. >
  116. Forgot password?
  117. </router-link>
  118. </p>
  119. <br />
  120. <p>
  121. By logging in you agree to our
  122. <router-link to="/terms" @click="closeCurrentModal()">
  123. Terms of Service
  124. </router-link>
  125. and
  126. <router-link to="/privacy" @click="closeCurrentModal()">
  127. Privacy Policy</router-link
  128. >.
  129. </p>
  130. </form>
  131. </template>
  132. <template #footer>
  133. <div id="actions">
  134. <button class="button is-primary" @click="submitModal()">
  135. Login
  136. </button>
  137. <a
  138. v-if="githubAuthentication"
  139. class="button is-github"
  140. :href="configStore.urls.api + '/auth/github/authorize'"
  141. @click="githubRedirect()"
  142. >
  143. <div class="icon">
  144. <img
  145. class="invert"
  146. src="/assets/social/github.svg"
  147. />
  148. </div>
  149. &nbsp;&nbsp;Login with GitHub
  150. </a>
  151. </div>
  152. <p
  153. v-if="!registrationDisabled"
  154. class="content-box-optional-helper"
  155. >
  156. <a @click="changeToRegisterModal()">
  157. Don't have an account?
  158. </a>
  159. </p>
  160. </template>
  161. </modal>
  162. </div>
  163. </template>
  164. <style lang="less" scoped>
  165. .night-mode {
  166. .modal-card,
  167. .modal-card-head,
  168. .modal-card-body,
  169. .modal-card-foot {
  170. background-color: var(--dark-grey-3);
  171. }
  172. .label,
  173. p:not(.help) {
  174. color: var(--light-grey-2);
  175. }
  176. }
  177. .modal-card-foot {
  178. display: flex;
  179. justify-content: space-between;
  180. flex-wrap: wrap;
  181. .content-box-optional-helper {
  182. margin-top: 0;
  183. }
  184. }
  185. .button.is-github {
  186. background-color: var(--dark-grey-2);
  187. color: var(--white) !important;
  188. }
  189. .is-github:focus {
  190. background-color: var(--dark-grey-4);
  191. }
  192. .is-primary:focus {
  193. background-color: var(--primary-color) !important;
  194. }
  195. .invert {
  196. filter: brightness(5);
  197. }
  198. </style>