From a85fc63c2afa6113ff77608eaf1af24a382e2857 Mon Sep 17 00:00:00 2001 From: simon bailly Date: Fri, 26 Jul 2024 11:01:33 +0400 Subject: [PATCH] GetRedirectResult to handle errors --- src/components/AppHeader.vue | 27 +++++++++++++++++++++++++-- src/components/AuthComponent.vue | 4 ++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/components/AppHeader.vue b/src/components/AppHeader.vue index 8507db2..1372ada 100644 --- a/src/components/AppHeader.vue +++ b/src/components/AppHeader.vue @@ -20,11 +20,11 @@ onAuthStateChanged(auth, (user) => { isAuthLoading.value = false if (user) { isLoggedIn.value = true - console.log('Successfully logged in !') + console.log('Logged in !') } else { isLoggedIn.value = false - console.log('Successfully logged out!'); + console.log('Logged out!'); } }); @@ -51,6 +51,29 @@ async function hideLogout() { // or show error message to the user (account disabled, etc). They can call this API to get that information. // GetRedirectResult +getRedirectResult(auth) + .then((result) => { + const credential = GithubAuthProvider.credentialFromResult(result); + if (credential) { + // This gives you a GitHub Access Token. You can use it to access the GitHub API. + const token = credential.accessToken; + // ... + } + + // The signed-in user info. + const user = result.user; + }).catch((error) => { + // Handle Errors here. + const errorCode = error.code; + const errorMessage = error.message; + alert(error.message) + // The email of the user's account used. + const email = error.email; + // The AuthCredential type that was used. + const credential = GithubAuthProvider.credentialFromError(error); + // ... + }); + onMounted(async () => { isAuthLoading.value = true }) diff --git a/src/components/AuthComponent.vue b/src/components/AuthComponent.vue index bd15fed..af3458c 100644 --- a/src/components/AuthComponent.vue +++ b/src/components/AuthComponent.vue @@ -17,13 +17,13 @@ const isAuthLoading = ref(false) const googleProvider = new GoogleAuthProvider() const githubProvider = new GithubAuthProvider() -// Google provider. Check GetRedirectResult in AppHeader Component to get results. +// Google provider. Check GetRedirectResult in to get results & handle errors. async function signInWithGoogle() { isAuthLoading.value = true await signInWithRedirect(auth, googleProvider) } -// GitHub provider. Check GetRedirectResult in AppHeader Component to get results. +// GitHub provider. Check GetRedirectResult in to get results & handle errors. async function signInWithGitHub() { isAuthLoading.value = true await signInWithRedirect(auth, githubProvider)