From 21038cc5ac03dabd4c18763336d943d3b4249a50 Mon Sep 17 00:00:00 2001 From: MartinBraquet Date: Thu, 30 Oct 2025 23:28:18 +0100 Subject: [PATCH] Clean and refactor oauth redirect --- web/lib/firebase/oauth.ts | 49 +++++++++++++++++++++++++++++++++++++ web/lib/firebase/users.ts | 2 -- web/pages/_app.tsx | 37 +--------------------------- web/pages/auth/callback.tsx | 14 +---------- 4 files changed, 51 insertions(+), 51 deletions(-) create mode 100644 web/lib/firebase/oauth.ts diff --git a/web/lib/firebase/oauth.ts b/web/lib/firebase/oauth.ts new file mode 100644 index 00000000..df3feccf --- /dev/null +++ b/web/lib/firebase/oauth.ts @@ -0,0 +1,49 @@ +import {unauthedApi} from "common/util/api"; +import {GoogleAuthProvider, signInWithCredential} from "firebase/auth"; +import {auth} from "web/lib/firebase/users"; + +export async function fetchToken() { + const params = new URLSearchParams(window.location.search); + console.log('/auth/callback', params); + const code = params.get('code'); + + if (code) { + // Send code back to the native app + window.location.href = `com.compassmeet://auth?code=${encodeURIComponent(code)}}`; + } else { + document.body.textContent = 'Missing code in redirect.'; + } +} + +export async function oauthRedirect(event: any) { + console.log('Received oauthRedirect event:', event); + const detail = event.data + console.log('OAuth data:', detail); + if (!detail) { + console.error('No detail found in event'); + return; + } + const url = new URL(detail); + + const code = url.searchParams.get('code'); + if (!code) { + console.error('No code found in URL'); + return; + } + + try { + const {result} = await unauthedApi('auth-google', {code}) + const googleTokens = result.tokens + // console.log('/auth-google tokens', googleTokens); + // Create a Firebase credential from the Google tokens + const credential = GoogleAuthProvider.credential(googleTokens.id_token, googleTokens.access_token) + // Sign in with Firebase using the credential + const userCredential = await signInWithCredential(auth, credential) + // console.log('Creds:', userCredential) + // console.log('Firebase user:', userCredential.user) + return userCredential + } catch (e) { + console.error('Error during OAuth flow:', e); + return + } +} \ No newline at end of file diff --git a/web/lib/firebase/users.ts b/web/lib/firebase/users.ts index f7790212..e4bb2e3d 100644 --- a/web/lib/firebase/users.ts +++ b/web/lib/firebase/users.ts @@ -57,7 +57,6 @@ export function writeReferralInfo( * @public */ export async function webviewGoogleSignin() { - const params = { client_id: GOOGLE_CLIENT_ID, redirect_uri: REDIRECT_URI, @@ -65,7 +64,6 @@ export async function webviewGoogleSignin() { scope: 'openid email profile', } console.log('params', params) - window.open(`https://accounts.google.com/o/oauth2/v2/auth?${new URLSearchParams(params)}`, '_system'); } diff --git a/web/pages/_app.tsx b/web/pages/_app.tsx index 642efd77..8b121841 100644 --- a/web/pages/_app.tsx +++ b/web/pages/_app.tsx @@ -12,12 +12,10 @@ import clsx from 'clsx' import {initTracking} from 'web/lib/service/analytics' import WebPush from "web/lib/service/web-push"; import AndroidPush from "web/lib/service/android-push"; -import {unauthedApi} from "common/util/api"; -import {GoogleAuthProvider, signInWithCredential} from "firebase/auth"; -import {auth} from "web/lib/firebase/users"; import {isAndroidWebView} from "web/lib/util/webview"; import {Capacitor} from '@capacitor/core'; import {StatusBar, Style} from '@capacitor/status-bar'; +import {oauthRedirect} from "web/lib/firebase/oauth"; if (Capacitor.isNativePlatform()) { // Only runs on iOS/Android native @@ -79,39 +77,6 @@ function MyApp({Component, pageProps}: AppProps) { }, []) useEffect(() => { - async function oauthRedirect(event: any) { - console.log('Received oauthRedirect event:', event); - const detail = event.data - console.log('OAuth data:', detail); - if (!detail) { - console.error('No detail found in event'); - return; - } - const url = new URL(detail); - - const code = url.searchParams.get('code'); - if (!code) { - console.error('No code found in URL'); - return; - } - - try { - const {result} = await unauthedApi('auth-google', {code}) - const googleTokens = result.tokens - // console.log('/auth-google tokens', googleTokens); - // Create a Firebase credential from the Google tokens - const credential = GoogleAuthProvider.credential(googleTokens.id_token, googleTokens.access_token) - // Sign in with Firebase using the credential - const userCredential = await signInWithCredential(auth, credential) - // console.log('Creds:', userCredential) - // console.log('Firebase user:', userCredential.user) - return userCredential - } catch (e) { - console.error('Error during OAuth flow:', e); - return - } - } - // Expose globally for native bridge (window as any).oauthRedirect = oauthRedirect; }, []); diff --git a/web/pages/auth/callback.tsx b/web/pages/auth/callback.tsx index 8d2eae7c..641ec5a8 100644 --- a/web/pages/auth/callback.tsx +++ b/web/pages/auth/callback.tsx @@ -1,20 +1,8 @@ import {useEffect} from "react"; +import {fetchToken} from "web/lib/firebase/oauth"; export default function GoogleAuthCallback() { useEffect(() => { - async function fetchToken() { - const params = new URLSearchParams(window.location.search); - console.log('/auth/callback', params); - const code = params.get('code'); - - if (code) { - // Send code back to the native app - window.location.href = `com.compassmeet://auth?code=${encodeURIComponent(code)}}`; - } else { - document.body.textContent = 'Missing code in redirect.'; - } - } - fetchToken() }, []); }