|
| 1 | +<script setup lang="ts"> |
| 2 | +import { |
| 3 | + getAuth, |
| 4 | + GoogleAuthProvider, |
| 5 | + GithubAuthProvider, |
| 6 | + EmailAuthProvider, |
| 7 | +} from "firebase/auth"; |
| 8 | +
|
| 9 | +const props = withDefaults( |
| 10 | + defineProps<{ |
| 11 | + to?: string; |
| 12 | + }>(), |
| 13 | + { to: "/" }, |
| 14 | +); |
| 15 | +
|
| 16 | +const router = useRouter(); |
| 17 | +const authStore = useAuthStore(); |
| 18 | +const notificationsStore = useNotificationsStore(); |
| 19 | +const appStore = useAppStore(); |
| 20 | +const authContainer = ref<HTMLElement | null>(null); |
| 21 | +const firebaseUIInitialized = ref(false); |
| 22 | +let ui: any = null; |
| 23 | +
|
| 24 | +onMounted(async () => { |
| 25 | + if (!import.meta.client) return; |
| 26 | +
|
| 27 | + const firebaseui = await import("firebaseui"); |
| 28 | + await import("firebaseui/dist/firebaseui.css"); |
| 29 | +
|
| 30 | + const auth = getAuth(); |
| 31 | + ui = new firebaseui.auth.AuthUI(auth); |
| 32 | + ui.start("#firebaseui-auth-container", { |
| 33 | + callbacks: { |
| 34 | + signInSuccessWithAuthResult: (authResult: any) => { |
| 35 | + notificationsStore.addNotification({ |
| 36 | + message: "Login successful!", |
| 37 | + type: "success", |
| 38 | + }); |
| 39 | + authStore.onAuthStateChanged(authResult.user); |
| 40 | + router.push({ path: props.to }); |
| 41 | + return false; |
| 42 | + }, |
| 43 | + uiShown: () => { |
| 44 | + firebaseUIInitialized.value = true; |
| 45 | + if (authContainer.value) { |
| 46 | + Array.from( |
| 47 | + authContainer.value.getElementsByClassName( |
| 48 | + "firebaseui-idp-text-long", |
| 49 | + ), |
| 50 | + ).forEach((item: Element) => { |
| 51 | + item.textContent = |
| 52 | + item.textContent?.replace("Sign in with", "Continue with") || |
| 53 | + null; |
| 54 | + }); |
| 55 | + } |
| 56 | + }, |
| 57 | + }, |
| 58 | + signInFlow: "popup", |
| 59 | + signInSuccessUrl: window.location.href, |
| 60 | + signInOptions: [ |
| 61 | + GoogleAuthProvider.PROVIDER_ID, |
| 62 | + GithubAuthProvider.PROVIDER_ID, |
| 63 | + EmailAuthProvider.PROVIDER_ID, |
| 64 | + ], |
| 65 | + tosUrl: appStore.appData.url + "/terms-and-conditions", |
| 66 | + privacyPolicyUrl: appStore.appData.url + "/privacy-policy", |
| 67 | + }); |
| 68 | +}); |
| 69 | +
|
| 70 | +onBeforeUnmount(() => { |
| 71 | + if (ui) ui.delete(); |
| 72 | +}); |
| 73 | +</script> |
| 74 | + |
| 75 | +<template> |
| 76 | + <div> |
| 77 | + <div id="firebaseui-auth-container" ref="authContainer" /> |
| 78 | + <v-progress-circular |
| 79 | + v-if="!firebaseUIInitialized" |
| 80 | + class="mx-auto d-block my-16" |
| 81 | + :size="80" |
| 82 | + :width="5" |
| 83 | + color="primary" |
| 84 | + indeterminate |
| 85 | + /> |
| 86 | + </div> |
| 87 | +</template> |
0 commit comments