Skip to content

Commit 0dd3bac

Browse files
committed
Add googleNativeLogin
1 parent d7a716a commit 0dd3bac

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

web/lib/firebase/users.ts

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
import { type User } from 'common/user'
1+
import {type User} from 'common/user'
22
import dayjs from 'dayjs'
33
import utc from 'dayjs/plugin/utc'
4-
import {
5-
GoogleAuthProvider,
6-
OAuthProvider,
7-
getAuth,
8-
signInWithPopup,
9-
} from 'firebase/auth'
10-
import { safeLocalStorage } from '../util/local'
11-
import { app } from './init'
4+
import {getAuth, GoogleAuthProvider, OAuthProvider, signInWithCredential, signInWithPopup,} from 'firebase/auth'
5+
6+
import {safeLocalStorage} from '../util/local'
7+
import {app} from './init'
8+
import {SocialLogin} from "@capgo/capacitor-social-login";
9+
import {Capacitor} from "@capacitor/core";
1210

1311
dayjs.extend(utc)
1412

15-
export type { User }
13+
export type {User}
1614

1715
export const auth = getAuth(app)
1816

@@ -31,7 +29,7 @@ export function writeReferralInfo(
3129
) {
3230
const local = safeLocalStorage
3331
const cachedReferralUser = local?.getItem(CACHED_REFERRAL_USERNAME_KEY)
34-
const { explicitReferrer } = otherOptions || {}
32+
const {explicitReferrer} = otherOptions || {}
3533

3634
// Write the first referral username we see.
3735
if (!cachedReferralUser) {
@@ -47,7 +45,45 @@ export function writeReferralInfo(
4745
}
4846
}
4947

48+
49+
export async function googleNativeLogin() {
50+
await SocialLogin.initialize({
51+
google: {
52+
webClientId: '253367029065-khkj31qt22l0vc3v754h09vhpg6t33ad.apps.googleusercontent.com', // Required for Android and Web
53+
// iOSClientId: 'YOUR_IOS_CLIENT_ID', // Required for iOS
54+
// iOSServerClientId: 'YOUR_WEB_CLIENT_ID', // Required for iOS offline mode and server authorization (same as webClientId)
55+
mode: 'online', // 'online' or 'offline'
56+
}
57+
});
58+
// Run the native Google OAuth
59+
const result: any = await SocialLogin.login({provider: 'google', options: {}})
60+
61+
console.log('result', result)
62+
63+
// Extract the tokens from the native result
64+
const idToken = result?.result?.idToken
65+
const accessToken = result?.result?.accessToken?.token
66+
67+
if (!idToken) {
68+
throw new Error('No idToken returned from Google login')
69+
}
70+
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
80+
}
81+
82+
5083
export async function firebaseLogin() {
84+
if (Capacitor.isNativePlatform()) {
85+
return await googleNativeLogin()
86+
}
5187
const provider = new GoogleAuthProvider()
5288
return signInWithPopup(auth, provider).then(async (result) => {
5389
return result

0 commit comments

Comments
 (0)