Skip to content

Commit 21038cc

Browse files
committed
Clean and refactor oauth redirect
1 parent d6749bc commit 21038cc

File tree

4 files changed

+51
-51
lines changed

4 files changed

+51
-51
lines changed

web/lib/firebase/oauth.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {unauthedApi} from "common/util/api";
2+
import {GoogleAuthProvider, signInWithCredential} from "firebase/auth";
3+
import {auth} from "web/lib/firebase/users";
4+
5+
export async function fetchToken() {
6+
const params = new URLSearchParams(window.location.search);
7+
console.log('/auth/callback', params);
8+
const code = params.get('code');
9+
10+
if (code) {
11+
// Send code back to the native app
12+
window.location.href = `com.compassmeet://auth?code=${encodeURIComponent(code)}}`;
13+
} else {
14+
document.body.textContent = 'Missing code in redirect.';
15+
}
16+
}
17+
18+
export async function oauthRedirect(event: any) {
19+
console.log('Received oauthRedirect event:', event);
20+
const detail = event.data
21+
console.log('OAuth data:', detail);
22+
if (!detail) {
23+
console.error('No detail found in event');
24+
return;
25+
}
26+
const url = new URL(detail);
27+
28+
const code = url.searchParams.get('code');
29+
if (!code) {
30+
console.error('No code found in URL');
31+
return;
32+
}
33+
34+
try {
35+
const {result} = await unauthedApi('auth-google', {code})
36+
const googleTokens = result.tokens
37+
// console.log('/auth-google tokens', googleTokens);
38+
// Create a Firebase credential from the Google tokens
39+
const credential = GoogleAuthProvider.credential(googleTokens.id_token, googleTokens.access_token)
40+
// Sign in with Firebase using the credential
41+
const userCredential = await signInWithCredential(auth, credential)
42+
// console.log('Creds:', userCredential)
43+
// console.log('Firebase user:', userCredential.user)
44+
return userCredential
45+
} catch (e) {
46+
console.error('Error during OAuth flow:', e);
47+
return
48+
}
49+
}

web/lib/firebase/users.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,13 @@ export function writeReferralInfo(
5757
* @public
5858
*/
5959
export async function webviewGoogleSignin() {
60-
6160
const params = {
6261
client_id: GOOGLE_CLIENT_ID,
6362
redirect_uri: REDIRECT_URI,
6463
response_type: 'code',
6564
scope: 'openid email profile',
6665
}
6766
console.log('params', params)
68-
6967
window.open(`https://accounts.google.com/o/oauth2/v2/auth?${new URLSearchParams(params)}`, '_system');
7068
}
7169

web/pages/_app.tsx

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ import clsx from 'clsx'
1212
import {initTracking} from 'web/lib/service/analytics'
1313
import WebPush from "web/lib/service/web-push";
1414
import AndroidPush from "web/lib/service/android-push";
15-
import {unauthedApi} from "common/util/api";
16-
import {GoogleAuthProvider, signInWithCredential} from "firebase/auth";
17-
import {auth} from "web/lib/firebase/users";
1815
import {isAndroidWebView} from "web/lib/util/webview";
1916
import {Capacitor} from '@capacitor/core';
2017
import {StatusBar, Style} from '@capacitor/status-bar';
18+
import {oauthRedirect} from "web/lib/firebase/oauth";
2119

2220
if (Capacitor.isNativePlatform()) {
2321
// Only runs on iOS/Android native
@@ -79,39 +77,6 @@ function MyApp({Component, pageProps}: AppProps<PageProps>) {
7977
}, [])
8078

8179
useEffect(() => {
82-
async function oauthRedirect(event: any) {
83-
console.log('Received oauthRedirect event:', event);
84-
const detail = event.data
85-
console.log('OAuth data:', detail);
86-
if (!detail) {
87-
console.error('No detail found in event');
88-
return;
89-
}
90-
const url = new URL(detail);
91-
92-
const code = url.searchParams.get('code');
93-
if (!code) {
94-
console.error('No code found in URL');
95-
return;
96-
}
97-
98-
try {
99-
const {result} = await unauthedApi('auth-google', {code})
100-
const googleTokens = result.tokens
101-
// console.log('/auth-google tokens', googleTokens);
102-
// Create a Firebase credential from the Google tokens
103-
const credential = GoogleAuthProvider.credential(googleTokens.id_token, googleTokens.access_token)
104-
// Sign in with Firebase using the credential
105-
const userCredential = await signInWithCredential(auth, credential)
106-
// console.log('Creds:', userCredential)
107-
// console.log('Firebase user:', userCredential.user)
108-
return userCredential
109-
} catch (e) {
110-
console.error('Error during OAuth flow:', e);
111-
return
112-
}
113-
}
114-
11580
// Expose globally for native bridge
11681
(window as any).oauthRedirect = oauthRedirect;
11782
}, []);

web/pages/auth/callback.tsx

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
import {useEffect} from "react";
2+
import {fetchToken} from "web/lib/firebase/oauth";
23

34
export default function GoogleAuthCallback() {
45
useEffect(() => {
5-
async function fetchToken() {
6-
const params = new URLSearchParams(window.location.search);
7-
console.log('/auth/callback', params);
8-
const code = params.get('code');
9-
10-
if (code) {
11-
// Send code back to the native app
12-
window.location.href = `com.compassmeet://auth?code=${encodeURIComponent(code)}}`;
13-
} else {
14-
document.body.textContent = 'Missing code in redirect.';
15-
}
16-
}
17-
186
fetchToken()
197
}, []);
208
}

0 commit comments

Comments
 (0)