@@ -43,15 +43,58 @@ export function writeReferralInfo(
4343 }
4444}
4545
46- // export function isAndroidWebView() {
47- // try {
48- // // Detect if Android bridge exists
49- // return typeof (window as any).AndroidBridge?.isNativeApp === 'function';
50- // } catch {
51- // return false;
52- // }
53- // }
46+ export function isAndroidWebView ( ) {
47+ try {
48+ // Detect if Android bridge exists
49+ return typeof ( window as any ) . AndroidBridge ?. isNativeApp === 'function' ;
50+ } catch {
51+ return false ;
52+ }
53+ }
5454
55+ async function generatePKCE ( ) {
56+ const array = new Uint8Array ( 32 ) ;
57+ crypto . getRandomValues ( array ) ;
58+ const codeVerifier = btoa ( String . fromCharCode ( ...array ) )
59+ . replace ( / \+ / g, '-' ) . replace ( / \/ / g, '_' ) . replace ( / = + $ / , '' ) ;
60+
61+ const encoder = new TextEncoder ( ) ;
62+ const hashBuffer = await crypto . subtle . digest ( 'SHA-256' , encoder . encode ( codeVerifier ) ) ;
63+ const hashArray = Array . from ( new Uint8Array ( hashBuffer ) ) ;
64+ const codeChallenge = btoa ( String . fromCharCode ( ...hashArray ) )
65+ . replace ( / \+ / g, '-' ) . replace ( / \/ / g, '_' ) . replace ( / = + $ / , '' ) ;
66+
67+ console . log ( { codeVerifier, codeChallenge} )
68+ return { codeVerifier, codeChallenge} ;
69+ }
70+
71+ export const GOOGLE_CLIENT_ID = '253367029065-khkj31qt22l0vc3v754h09vhpg6t33ad.apps.googleusercontent.com'
72+
73+ /**
74+ * Authenticates a Firebase client running a webview APK on Android with Google OAuth.
75+ *
76+ * `https://accounts.google.com/o/oauth2/v2/auth?${params}` to get the code (in external browser, as google blocks it in webview)
77+ * Redirects to `com.compassmeet:/auth` (in webview java main activity)
78+ * 'https://oauth2.googleapis.com/token' to get the ID token (in javascript app)
79+ * signInWithCredential(auth, credential) to set up firebase user in client (auth.currentUser)
80+ *
81+ * @public
82+ */
83+ export async function webviewGoogleSignin ( ) {
84+ const { codeVerifier, codeChallenge} = await generatePKCE ( ) ;
85+ localStorage . setItem ( 'pkce_verifier' , codeVerifier ) ;
86+
87+ const params = new URLSearchParams ( {
88+ client_id : GOOGLE_CLIENT_ID ,
89+ redirect_uri : 'com.compassmeet:/auth' , // your deep link
90+ response_type : 'code' ,
91+ scope : 'openid email profile' ,
92+ code_challenge : codeChallenge ,
93+ code_challenge_method : 'S256' ,
94+ } ) ;
95+
96+ window . open ( `https://accounts.google.com/o/oauth2/v2/auth?${ params } ` , '_system' ) ;
97+ }
5598
5699// export async function googleNativeLogin() {
57100// console.log('Platform:', Capacitor.getPlatform())
@@ -94,12 +137,11 @@ export function writeReferralInfo(
94137// export const isRunningInAPK = () => typeof window !== 'undefined' && (window as any).IS_APK === true
95138
96139export async function firebaseLogin ( ) {
97- // if (isAndroidWebView()) {
98- // console.log('Running in APK')
99- // return await googleNativeLogin()
100- // return await signInWithRedirect(auth, new GoogleAuthProvider())
101- // }
102- // console.log('Running in web')
140+ if ( isAndroidWebView ( ) ) {
141+ console . log ( 'Running in APK' )
142+ return await webviewGoogleSignin ( )
143+ }
144+ console . log ( 'Running in web' )
103145 const provider = new GoogleAuthProvider ( )
104146 return signInWithPopup ( auth , provider ) . then ( async ( result ) => {
105147 return result
0 commit comments