From 564c1bbc00b75e014e0ac2c62029d9a745843be2 Mon Sep 17 00:00:00 2001 From: Jun-Murakami Date: Fri, 4 Oct 2024 11:16:43 +0900 Subject: [PATCH] feat: Update authentication domain for Firebase hosting workflows Update the authentication domain in the Firebase hosting workflows to use the correct domain for the production and test branches. This ensures that the authentication functionality works correctly in the deployed application. --- .github/workflows/firebase-hosting-merge.yml | 2 +- .../firebase-hosting-test-branch.yml | 2 +- src/features/homepage/hooks/useAuth.ts | 53 +++++++++++++------ 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/.github/workflows/firebase-hosting-merge.yml b/.github/workflows/firebase-hosting-merge.yml index c0dfb6e..d4afa7b 100644 --- a/.github/workflows/firebase-hosting-merge.yml +++ b/.github/workflows/firebase-hosting-merge.yml @@ -21,7 +21,7 @@ jobs: - run: npm run build env: VITE_API_KEY: ${{ secrets.VITE_API_KEY_PROD }} - VITE_AUTH_DOMAIN: ${{ secrets.VITE_AUTH_DOMAIN_PROD }} + VITE_AUTH_DOMAIN: tasktree-s.web.app VITE_DATABASE_URL: ${{ secrets.VITE_DATABASE_URL_PROD }} VITE_PROJECT_ID: ${{ secrets.VITE_PROJECT_ID_PROD }} VITE_STORAGE_BUCKET: ${{ secrets.VITE_STORAGE_BUCKET_PROD }} diff --git a/.github/workflows/firebase-hosting-test-branch.yml b/.github/workflows/firebase-hosting-test-branch.yml index 0d5f7d6..5a4ee22 100644 --- a/.github/workflows/firebase-hosting-test-branch.yml +++ b/.github/workflows/firebase-hosting-test-branch.yml @@ -18,7 +18,7 @@ jobs: - run: npm run build env: VITE_API_KEY: ${{ secrets.VITE_API_KEY }} - VITE_AUTH_DOMAIN: ${{ secrets.VITE_AUTH_DOMAIN }} + VITE_AUTH_DOMAIN: tasktrees-fb.web.app VITE_DATABASE_URL: ${{ secrets.VITE_DATABASE_URL }} VITE_PROJECT_ID: ${{ secrets.VITE_PROJECT_ID }} VITE_STORAGE_BUCKET: ${{ secrets.VITE_STORAGE_BUCKET }} diff --git a/src/features/homepage/hooks/useAuth.ts b/src/features/homepage/hooks/useAuth.ts index 39860c0..abc02ef 100644 --- a/src/features/homepage/hooks/useAuth.ts +++ b/src/features/homepage/hooks/useAuth.ts @@ -6,13 +6,14 @@ import { initializeAuth, GoogleAuthProvider, OAuthProvider, - signInWithPopup, signInWithCredential, signInWithEmailAndPassword, createUserWithEmailAndPassword, sendPasswordResetEmail, signOut, Unsubscribe, + signInWithRedirect, + getRedirectResult } from 'firebase/auth'; import { getDatabase, remove, ref, get, set } from 'firebase/database'; import { getStorage, ref as storageRef, deleteObject, listAll } from 'firebase/storage'; @@ -119,30 +120,52 @@ export const useAuth = () => { unsubscribe(); } }; - }, [loginAction, showDialog]); + }, [loginAction, showDialog, setIsLoggedIn, setIsLoading]); + + //リダイレクト状態の監視 + useEffect(() => { + getRedirectResult(getAuth()).then(async (userCredential) => { + console.log(userCredential); + }).catch((error) => { + console.error(error); + }); + }, [setIsLoggedIn, setIsLoading]); // Googleログイン const handleGoogleLogin = async () => { setIsLoading(true); setSystemMessage('ログイン中...'); - // 1. Create credentials on the native layer - try { - const result = await FirebaseAuthentication.signInWithGoogle(); - // 2. Sign in on the web layer using the id token - const credential = GoogleAuthProvider.credential(result.credential?.idToken); - await signInWithCredential(auth, credential) - .then(async () => { + if (Capacitor.isNativePlatform() && FirebaseAuthentication) { + // 1. Create credentials on the native layer + try { + const result = await FirebaseAuthentication.signInWithGoogle(); + // 2. Sign in on the web layer using the id token + const credential = GoogleAuthProvider.credential(result.credential?.idToken); + await signInWithCredential(auth, credential) + .then(async () => { + setIsLoggedIn(true); + setSystemMessage(null); + }) + .catch((error) => { + setSystemMessage('Googleログインに失敗しました。Code:100\n\n' + error.code); + setIsLoading(false); + }); + } catch (error) { + setSystemMessage('Googleログインに失敗しました。Code:101\n\n' + error); + setIsLoading(false); + return; + } + } else { + const provider = new OAuthProvider('google.com'); + signInWithRedirect(getAuth(), provider) + .then(() => { setIsLoggedIn(true); setSystemMessage(null); }) .catch((error) => { - setSystemMessage('Googleログインに失敗しました。Code:100\n\n' + error.code); + setSystemMessage('Googleログインに失敗しました。Code:102\n\n' + error.code); setIsLoading(false); }); - } catch (error) { - setSystemMessage('Googleログインに失敗しました。Code:101\n\n' + error); - setIsLoading(false); - return; } }; @@ -178,7 +201,7 @@ export const useAuth = () => { } } else { const provider = new OAuthProvider('apple.com'); - signInWithPopup(getAuth(), provider) + signInWithRedirect(getAuth(), provider) .then(() => { setIsLoggedIn(true); setSystemMessage(null);