Skip to content

Commit b121d61

Browse files
committed
Fix
1 parent 6ba3c3f commit b121d61

File tree

3 files changed

+47
-42
lines changed

3 files changed

+47
-42
lines changed

common/src/envs/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ console.debug(`Running in ${HOSTING_ENV} (${ENV})`,);
5858
// }
5959

6060
export const DOMAIN = IS_LOCAL ? LOCAL_WEB_DOMAIN : ENV_CONFIG.domain
61+
const protocol = IS_LOCAL ? 'http' : 'https'
62+
export const WEB_URL = `${protocol}://${DOMAIN}`
6163
export const BACKEND_DOMAIN = IS_LOCAL ? LOCAL_BACKEND_DOMAIN : ENV_CONFIG.backendDomain
6264
export const FIREBASE_CONFIG = ENV_CONFIG.firebaseConfig
6365
export const PROJECT_ID = ENV_CONFIG.firebaseConfig.projectId

web/pages/_app.tsx

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ 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 {GOOGLE_CLIENT_ID} from "web/lib/firebase/users";
15+
import {GoogleAuthProvider, signInWithCredential} from "firebase/auth";
16+
import {auth} from "web/lib/firebase/users";
1617

1718
// See https://nextjs.org/docs/basic-features/font-optimization#google-fonts
1819
// and if you add a font, you must add it to tailwind config as well for it to work.
@@ -66,37 +67,16 @@ function MyApp({Component, pageProps}: AppProps<PageProps>) {
6667

6768
useEffect(() => {
6869
async function oauthRedirect(event: any) {
69-
console.log('Received oauthRedirect event:', event);
70-
const detail = event.data
71-
console.log('OAuth data:', detail);
72-
if (!detail) {
73-
console.error('No detail found in event');
74-
return;
75-
}
76-
const url = new URL(detail);
77-
78-
const code = url.searchParams.get('code');
79-
if (!code) {
80-
console.error('No code found in URL');
81-
return;
82-
}
83-
84-
const codeVerifier = localStorage.getItem('pkce_verifier');
85-
86-
const tokenResponse = await fetch('https://oauth2.googleapis.com/token', {
87-
method: 'POST',
88-
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
89-
body: new URLSearchParams({
90-
client_id: GOOGLE_CLIENT_ID,
91-
code,
92-
code_verifier: codeVerifier!,
93-
redirect_uri: 'com.compassmeet://auth',
94-
grant_type: 'authorization_code',
95-
}),
96-
});
97-
98-
const tokens = await tokenResponse.json();
99-
console.log('Tokens:', tokens);
70+
const {idToken, accessToken} = event
71+
// Create a Firebase credential from the Google tokens
72+
const credential = GoogleAuthProvider.credential(idToken, accessToken)
73+
74+
// Sign in with Firebase using the credential
75+
const userCredential = await signInWithCredential(auth, credential)
76+
77+
console.log('Firebase user:', userCredential.user)
78+
79+
return userCredential
10080
}
10181

10282
// Expose globally for native bridge

web/pages/auth/callback.tsx

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
11
import {useEffect} from "react";
2+
import {GOOGLE_CLIENT_ID} from "web/lib/firebase/users";
23

34
export default function GoogleAuthCallback() {
45
useEffect(() => {
5-
const params = new URLSearchParams(window.location.search);
6-
const code = params.get('code');
7-
const state = params.get('state');
8-
console.log('/auth/callback code', code);
6+
async function fetchToken() {
7+
const params = new URLSearchParams(window.location.search);
8+
const code = params.get('code');
9+
const state = params.get('state');
10+
console.log('/auth/callback code', code);
911

10-
if (code) {
11-
// Send code back to native app
12-
const deepLink = `com.compassmeet://auth?code=${encodeURIComponent(code)}&state=${encodeURIComponent(state || '')}`;
13-
window.location.href = deepLink;
14-
} else {
15-
document.body.textContent = 'Missing code in redirect.';
12+
if (code) {
13+
const codeVerifier = localStorage.getItem('pkce_verifier');
14+
const body = new URLSearchParams({
15+
client_id: GOOGLE_CLIENT_ID,
16+
code,
17+
code_verifier: codeVerifier!,
18+
redirect_uri: 'com.compassmeet://auth',
19+
grant_type: 'authorization_code',
20+
});
21+
console.log('Body:', body);
22+
const tokenResponse = await fetch('https://oauth2.googleapis.com/token', {
23+
method: 'POST',
24+
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
25+
body: body,
26+
});
27+
const tokens = await tokenResponse.json();
28+
console.log('Tokens:', tokens);
29+
30+
// Send code back to the native app
31+
// const deepLink = `com.compassmeet://auth?tokens=${encodeURIComponent(tokens)}}`;
32+
// window.location.href = deepLink;
33+
34+
} else {
35+
document.body.textContent = 'Missing code in redirect.';
36+
}
1637
}
38+
39+
fetchToken()
1740
}, []);
1841
}

0 commit comments

Comments
 (0)