From cfa1987c3a0d4965550d47670967c76537b6439d Mon Sep 17 00:00:00 2001 From: Marco Crapts Date: Fri, 2 Jun 2023 16:00:39 +0200 Subject: [PATCH] custom auth --- src/components/Auth.vue | 65 ++++++++++++++++--- src/components/TopBar.vue | 15 +++-- .../TopBar/Settings/AccountSettings.vue | 6 +- src/composables/useFirebase.ts | 32 ++++++++- 4 files changed, 99 insertions(+), 19 deletions(-) diff --git a/src/components/Auth.vue b/src/components/Auth.vue index 402c634..70cdf50 100644 --- a/src/components/Auth.vue +++ b/src/components/Auth.vue @@ -2,10 +2,17 @@ import firebase from 'firebase/compat/app' import 'firebase/compat/auth' import 'firebaseui/dist/firebaseui.css' +import { + getAuth, + signInWithRedirect, + GoogleAuthProvider, + GithubAuthProvider, + OAuthProvider +} from 'firebase/auth' -const props = defineProps<{ - authenticating?: boolean -}>() +// const props = defineProps<{ +// authenticating?: boolean +// }>() const emit = defineEmits<{ signedIn: [authResult: any] @@ -15,8 +22,8 @@ const ui: any = inject('firebaseAuthUI') const uiConfig = { signInOptions: [ - firebase.auth.EmailAuthProvider.PROVIDER_ID, - firebase.auth.GoogleAuthProvider.PROVIDER_ID + firebase.auth.EmailAuthProvider.PROVIDER_ID + // firebase.auth.GoogleAuthProvider.PROVIDER_ID ], // signInFlow: 'popup', signInFlow: 'redirect', @@ -39,12 +46,52 @@ const uiConfig = { } // Other config options... } -onMounted(() => ui.start('#auth', uiConfig)) +// onMounted(() => ui.start('#auth', uiConfig)) + +type AuthProvider = 'google' | 'github' | 'microsoft' +const providers: { [key in AuthProvider]: any | (() => any) } = { + google: new GoogleAuthProvider(), + github: () => { + const provider = new GithubAuthProvider() + provider.addScope('user:email') + return provider + }, + microsoft: new OAuthProvider('microsoft.com') +} + +const signIn = (providerName: AuthProvider) => { + const auth = getAuth() + const provider = + typeof providers[providerName] === 'function' + ? providers[providerName]() + : providers[providerName] + signInWithRedirect(auth, provider) +} + +const authenticatingWithEmail = ref(false) +const signInWithEmail = () => { + authenticatingWithEmail.value = true + ui.start('#auth', uiConfig) +} diff --git a/src/components/TopBar.vue b/src/components/TopBar.vue index 15d8772..5334ee7 100644 --- a/src/components/TopBar.vue +++ b/src/components/TopBar.vue @@ -21,12 +21,13 @@ const emit = defineEmits<{ const searchActive = ref(false) -const authUI: any = inject('firebaseAuthUI') -const authPending = ref(authUI.isPendingRedirect()) +// const authUI: any = inject('firebaseAuthUI') +// const authPending = ref(authUI.isPendingRedirect()) +// const authPending = ref(false) const handleSignIn = async (close: () => Promise) => { await close() - authPending.value = false + // authPending.value = false }