Skip to content

Commit cae4b15

Browse files
committed
Fix
1 parent e41bc64 commit cae4b15

8 files changed

Lines changed: 59 additions & 31 deletions

File tree

backend/api/src/app.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import {IS_LOCAL} from "common/envs/constants";
6969
import {localSendTestEmail} from "api/test";
7070
import path from "node:path";
7171
import {saveSubscriptionMobile} from "api/save-subscription-mobile";
72+
import {authGoogle} from "api/auth-google";
7273

7374
// const corsOptions: CorsOptions = {
7475
// origin: ['*'], // Only allow requests from this domain
@@ -358,6 +359,7 @@ const handlers: { [k in APIPath]: APIHandler<k> } = {
358359
'save-subscription-mobile': saveSubscriptionMobile,
359360
'create-bookmarked-search': createBookmarkedSearch,
360361
'delete-bookmarked-search': deleteBookmarkedSearch,
362+
'auth-google': authGoogle,
361363
}
362364

363365
Object.entries(handlers).forEach(([path, handler]) => {

backend/api/src/auth-google.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {APIHandler} from './helpers/endpoint'
2+
import {GOOGLE_CLIENT_ID} from "common/constants";
3+
4+
export const authGoogle: APIHandler<'auth-google'> = async (
5+
{code},
6+
_auth
7+
) => {
8+
console.log('Google Auth Code:', code)
9+
if (!code) return {success: false, result: {}}
10+
11+
const body = {
12+
client_id: GOOGLE_CLIENT_ID,
13+
client_secret: process.env.GOOGLE_CLIENT_SECRET!,
14+
code: code as string,
15+
grant_type: 'authorization_code',
16+
redirect_uri: 'https://www.compassmeet.com/auth/callback',
17+
};
18+
console.log('Body:', body)
19+
const tokenRes = await fetch('https://oauth2.googleapis.com/token', {
20+
method: 'POST',
21+
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
22+
body: new URLSearchParams(body),
23+
});
24+
25+
const tokens = await tokenRes.json();
26+
console.log('Google Tokens:', tokens);
27+
28+
return {
29+
success: true,
30+
result: {tokens},
31+
}
32+
}

common/src/api/schema.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,17 @@ export const API = (_apiTypeCheck = {
733733
summary: 'Delete a bookmarked search by ID',
734734
tag: 'Searches',
735735
},
736+
'auth-google': {
737+
method: 'GET',
738+
authed: false,
739+
rateLimited: true,
740+
returns: {} as any,
741+
props: z.object({
742+
code: z.string(),
743+
}),
744+
summary: 'Google Auth',
745+
tag: 'Authentication',
746+
},
736747
} as const)
737748

738749
export type APIPath = keyof typeof API

common/src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ export const IS_MAINTENANCE = false // set to true to enable the maintenance mod
2424

2525
export const MIN_BIO_LENGTH = 250
2626

27+
export const WEB_GOOGLE_CLIENT_ID = '253367029065-khkj31qt22l0vc3v754h09vhpg6t33ad.apps.googleusercontent.com'
28+
export const ANDROID_GOOGLE_CLIENT_ID = '253367029065-s9sr5vqgkhc8f7p5s6ti6a4chqsrqgc4.apps.googleusercontent.com'
29+
export const GOOGLE_CLIENT_ID = WEB_GOOGLE_CLIENT_ID

common/src/secrets.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const secrets = (
2626
'VAPID_PUBLIC_KEY',
2727
'VAPID_PRIVATE_KEY',
2828
'DB_ENC_MASTER_KEY_BASE64',
29+
'GOOGLE_CLIENT_SECRET',
2930
// Some typescript voodoo to keep the string literal types while being not readonly.
3031
] as const
3132
).concat()

web/lib/firebase/users.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {getAuth, GoogleAuthProvider, signInWithPopup} from 'firebase/auth'
66
import {safeLocalStorage} from '../util/local'
77
import {app} from './init'
88
import {IS_LOCAL, WEB_URL} from "common/envs/constants";
9+
import {GOOGLE_CLIENT_ID} from "common/constants";
910

1011
dayjs.extend(utc)
1112

@@ -69,10 +70,6 @@ async function generatePKCE() {
6970
return {codeVerifier, codeChallenge};
7071
}
7172

72-
export const WEB_GOOGLE_CLIENT_ID = '253367029065-khkj31qt22l0vc3v754h09vhpg6t33ad.apps.googleusercontent.com'
73-
export const ANDROID_GOOGLE_CLIENT_ID = '253367029065-s9sr5vqgkhc8f7p5s6ti6a4chqsrqgc4.apps.googleusercontent.com'
74-
export const GOOGLE_CLIENT_ID = WEB_GOOGLE_CLIENT_ID
75-
7673
/**
7774
* Authenticates a Firebase client running a webview APK on Android with Google OAuth.
7875
*
@@ -88,7 +85,7 @@ export async function webviewGoogleSignin() {
8885
localStorage.setItem('pkce_verifier', codeVerifier);
8986

9087
const params = new URLSearchParams({
91-
client_id: ANDROID_GOOGLE_CLIENT_ID,
88+
client_id: GOOGLE_CLIENT_ID,
9289
redirect_uri: `${WEB_URL}/auth/callback`,
9390
response_type: 'code',
9491
scope: 'openid email profile',

web/pages/_app.tsx

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ 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 {ANDROID_GOOGLE_CLIENT_ID} from "web/lib/firebase/users";
15+
import {unauthedApi} from "common/util/api";
1616

1717
// See https://nextjs.org/docs/basic-features/font-optimization#google-fonts
1818
// and if you add a font, you must add it to tailwind config as well for it to work.
@@ -81,24 +81,9 @@ function MyApp({Component, pageProps}: AppProps<PageProps>) {
8181
return;
8282
}
8383

84-
const codeVerifier = localStorage.getItem('pkce_verifier');
85-
86-
const body = {
87-
client_id: ANDROID_GOOGLE_CLIENT_ID,
88-
code,
89-
code_verifier: codeVerifier!,
90-
redirect_uri: 'com.compassmeet:/auth',
91-
grant_type: 'authorization_code',
92-
}
93-
console.log('Body:', body);
94-
const tokenResponse = await fetch('https://oauth2.googleapis.com/token', {
95-
method: 'POST',
96-
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
97-
body: new URLSearchParams(body),
98-
});
99-
100-
const tokens = await tokenResponse.json();
101-
console.log('Tokens:', tokens);
84+
const {result} = await unauthedApi('auth-google', {code})
85+
console.log('/auth-google result', result);
86+
// google sign in
10287
}
10388

10489
// Expose globally for native bridge

web/pages/auth/callback.tsx

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

43
export default function GoogleAuthCallback() {
54
useEffect(() => {
65
async function fetchToken() {
76
const params = new URLSearchParams(window.location.search);
8-
console.log('/auth/callback code', params);
7+
console.log('/auth/callback', params);
98
const code = params.get('code');
10-
const state = params.get('state');
11-
console.log('/auth/callback code', code);
9+
// const state = params.get('state');
1210

1311
if (code) {
12+
console.log('/auth/callback code', code);
1413
// Send code back to the native app
15-
const deepLink = `com.compassmeet://auth?code=${encodeURIComponent(code)}&state=${encodeURIComponent(state || '')}`;
16-
window.location.href = deepLink;
14+
window.location.href = `com.compassmeet://auth?code=${encodeURIComponent(code)}}`;
1715

18-
//
1916
// const codeVerifier = localStorage.getItem('pkce_verifier');
2017
// const body = new URLSearchParams({
2118
// client_id: GOOGLE_CLIENT_ID,

0 commit comments

Comments
 (0)