|
| 1 | +<template> |
| 2 | + <form v-focustrap class="flex flex-col gap-4 relative max-w-md w-full text-sm rounded-md pt-9"> |
| 3 | + <div class="flex justify-center gap-2"> |
| 4 | + <a |
| 5 | + class="inline-block text-xl text-muted-color transition-all duration-300 hover:text-primary-400 hover:scale-150 cursor-pointer" |
| 6 | + @click="openWebAuthn" |
| 7 | + title="WebAuthn" |
| 8 | + > |
| 9 | + <i class="fa-solid fa-fingerprint" /> |
| 10 | + </a> |
| 11 | + <template v-if="oauths !== undefined"> |
| 12 | + <a |
| 13 | + v-for="oauth in oauths" |
| 14 | + :href="oauth.url" |
| 15 | + class="inline-block text-xl text-muted-color hover:scale-125 transition-all cursor-pointer hover:text-primary-400 mb-6" |
| 16 | + :title="oauth.provider" |
| 17 | + > |
| 18 | + <i class="items-center" :class="oauth.icon"></i> |
| 19 | + </a> |
| 20 | + </template> |
| 21 | + </div> |
| 22 | + <div class="inline-flex flex-col gap-2" :class="props.padding ?? 'px-9'"> |
| 23 | + <FloatLabel variant="on"> |
| 24 | + <InputText id="username" v-model="username" autocomplete="username" :autofocus="true" /> |
| 25 | + <label for="username">{{ $t("dialogs.login.username") }}</label> |
| 26 | + </FloatLabel> |
| 27 | + </div> |
| 28 | + <div class="inline-flex flex-col gap-2" :class="props.padding ?? 'px-9'"> |
| 29 | + <FloatLabel variant="on"> |
| 30 | + <InputPassword id="password" v-model="password" @keydown.enter="login" autocomplete="current-password" /> |
| 31 | + <label for="password">{{ $t("dialogs.login.password") }}</label> |
| 32 | + </FloatLabel> |
| 33 | + <Message v-if="invalidPassword" severity="error">{{ $t("dialogs.login.unknown_invalid") }}</Message> |
| 34 | + </div> |
| 35 | + <div class="text-muted-color text-right font-semibold" :class="props.padding ?? 'px-9'"> |
| 36 | + Lychee <span class="text-primary-500" v-if="is_se_enabled">SE</span> |
| 37 | + </div> |
| 38 | + <div class="flex items-center mt-9"> |
| 39 | + <Button |
| 40 | + v-if="closeCallback !== undefined" |
| 41 | + @click="props.closeCallback" |
| 42 | + severity="secondary" |
| 43 | + class="w-full font-bold border-none rounded-none rounded-bl-xl shrink" |
| 44 | + > |
| 45 | + {{ $t("dialogs.button.cancel") }} |
| 46 | + </Button> |
| 47 | + <Button |
| 48 | + @click="login" |
| 49 | + severity="contrast" |
| 50 | + :class="{ |
| 51 | + 'w-full font-bold border-none shrink': true, |
| 52 | + 'rounded-none rounded-br-xl': closeCallback !== undefined, |
| 53 | + 'rounded-xl': closeCallback === undefined, |
| 54 | + }" |
| 55 | + > |
| 56 | + {{ $t("dialogs.login.signin") }} |
| 57 | + </Button> |
| 58 | + </div> |
| 59 | + </form> |
| 60 | +</template> |
| 61 | +<script setup lang="ts"> |
| 62 | +import { ref } from "vue"; |
| 63 | +import FloatLabel from "primevue/floatlabel"; |
| 64 | +import Button from "primevue/button"; |
| 65 | +import Message from "primevue/message"; |
| 66 | +import AuthService from "@/services/auth-service"; |
| 67 | +import InputText from "@/components/forms/basic/InputText.vue"; |
| 68 | +import InputPassword from "@/components/forms/basic/InputPassword.vue"; |
| 69 | +import { useAuthStore } from "@/stores/Auth"; |
| 70 | +import AlbumService from "@/services/album-service"; |
| 71 | +import { useLycheeStateStore } from "@/stores/LycheeState"; |
| 72 | +import { storeToRefs } from "pinia"; |
| 73 | +import { useTogglablesStateStore } from "@/stores/ModalsState"; |
| 74 | +import { onMounted } from "vue"; |
| 75 | +
|
| 76 | +const emits = defineEmits<{ |
| 77 | + "logged-in": []; |
| 78 | +}>(); |
| 79 | +
|
| 80 | +type OauthProvider = { |
| 81 | + url: string; |
| 82 | + icon: string; |
| 83 | + provider: App.Enum.OauthProvidersType; |
| 84 | +}; |
| 85 | +
|
| 86 | +const props = defineProps<{ |
| 87 | + closeCallback?: () => void; |
| 88 | + padding?: string; |
| 89 | +}>(); |
| 90 | +
|
| 91 | +const username = ref(""); |
| 92 | +const password = ref(""); |
| 93 | +const authStore = useAuthStore(); |
| 94 | +const togglableStore = useTogglablesStateStore(); |
| 95 | +const lycheeStore = useLycheeStateStore(); |
| 96 | +const { is_se_enabled } = storeToRefs(lycheeStore); |
| 97 | +const { is_login_open, is_webauthn_open } = storeToRefs(togglableStore); |
| 98 | +const invalidPassword = ref(false); |
| 99 | +
|
| 100 | +const oauths = ref<OauthProvider[] | undefined>(undefined); |
| 101 | +
|
| 102 | +function login() { |
| 103 | + AuthService.login(username.value, password.value) |
| 104 | + .then(() => { |
| 105 | + is_login_open.value = false; |
| 106 | + authStore.setUser(null); |
| 107 | + invalidPassword.value = false; |
| 108 | + AlbumService.clearCache(); |
| 109 | + emits("logged-in"); |
| 110 | + }) |
| 111 | + .catch((e: any) => { |
| 112 | + if (e.response && e.response.status === 401) { |
| 113 | + invalidPassword.value = true; |
| 114 | + } |
| 115 | + }); |
| 116 | +} |
| 117 | +
|
| 118 | +function openWebAuthn() { |
| 119 | + is_login_open.value = false; |
| 120 | + is_webauthn_open.value = true; |
| 121 | + username.value = ""; |
| 122 | + password.value = ""; |
| 123 | + invalidPassword.value = false; |
| 124 | +} |
| 125 | +
|
| 126 | +onMounted(() => { |
| 127 | + authStore.getOauthData().then((data) => { |
| 128 | + oauths.value = data; |
| 129 | + }); |
| 130 | +}); |
| 131 | +</script> |
0 commit comments