Login.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <script setup lang="ts">
  2. import { useStore } from "vuex";
  3. import { ref, onMounted } from "vue";
  4. import { useRoute } from "vue-router";
  5. import Toast from "toasters";
  6. const route = useRoute();
  7. const email = ref("");
  8. const password = ref({
  9. value: "",
  10. visible: false
  11. });
  12. const apiDomain = ref("");
  13. const siteSettings = ref({
  14. registrationDisabled: false,
  15. githubAuthentication: false
  16. });
  17. const passwordElement = ref();
  18. const store = useStore();
  19. const login = payload => store.dispatch("user/auth/login", payload);
  20. const openModal = payload =>
  21. store.dispatch("modalVisibility/openModal", payload);
  22. const closeCurrentModal = () =>
  23. store.dispatch("modalVisibility/closeCurrentModal");
  24. const submitModal = () => {
  25. login({
  26. email: email.value,
  27. password: password.value.value
  28. })
  29. .then(res => {
  30. if (res.status === "success") window.location.reload();
  31. })
  32. .catch(err => new Toast(err.message));
  33. };
  34. const checkForAutofill = event => {
  35. if (
  36. event.target.value !== "" &&
  37. event.inputType === undefined &&
  38. event.data === undefined &&
  39. event.dataTransfer === undefined &&
  40. event.isComposing === undefined
  41. )
  42. submitModal();
  43. };
  44. const togglePasswordVisibility = () => {
  45. if (passwordElement.value.type === "password") {
  46. passwordElement.value.type = "text";
  47. password.value.visible = true;
  48. } else {
  49. passwordElement.value.type = "password";
  50. password.value.visible = false;
  51. }
  52. };
  53. const changeToRegisterModal = () => {
  54. closeCurrentModal();
  55. openModal("register");
  56. };
  57. const githubRedirect = () => {
  58. localStorage.setItem("github_redirect", route.path);
  59. };
  60. onMounted(async () => {
  61. apiDomain.value = await lofig.get("backend.apiDomain");
  62. siteSettings.value = await lofig.get("siteSettings");
  63. });
  64. </script>
  65. <template>
  66. <div>
  67. <modal
  68. title="Login"
  69. class="login-modal"
  70. :size="'slim'"
  71. @closed="closeCurrentModal()"
  72. >
  73. <template #body>
  74. <form>
  75. <!-- email address -->
  76. <p class="control">
  77. <label class="label">Username/Email</label>
  78. <input
  79. v-model="email"
  80. class="input"
  81. type="email"
  82. placeholder="Username/Email..."
  83. @keyup.enter="submitModal()"
  84. />
  85. </p>
  86. <!-- password -->
  87. <p class="control">
  88. <label class="label">Password</label>
  89. </p>
  90. <div id="password-visibility-container">
  91. <input
  92. v-model="password.value"
  93. class="input"
  94. type="password"
  95. ref="passwordElement"
  96. placeholder="Password..."
  97. @input="checkForAutofill($event)"
  98. @keyup.enter="submitModal()"
  99. />
  100. <a @click="togglePasswordVisibility()">
  101. <i class="material-icons">
  102. {{
  103. !password.visible
  104. ? "visibility"
  105. : "visibility_off"
  106. }}
  107. </i>
  108. </a>
  109. </div>
  110. <p class="content-box-optional-helper">
  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="siteSettings.githubAuthentication"
  139. class="button is-github"
  140. :href="apiDomain + '/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="!siteSettings.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>