Google Auth Migration
Questo contenuto non è ancora disponibile nella tua lingua.
Migration Guide from @codetrix-studio/capacitor-google-auth to @capgo/capacitor-social-login
Installation
-
Remove the old package:
Terminal window npm uninstall @codetrix-studio/capacitor-google-auth -
Install the new package:
Terminal window npm install @capgo/capacitor-social-loginnpx cap sync
Important Changes in Google Auth Setup
Web Client ID
The plugin now uses exclusively the Web Client ID for authentication. You’ll need to:
- Create a Web Client ID in Google Cloud Console if you don’t have one (How to get the credentials)
- Use this Web Client ID in the
webClientId
field for all platforms - For Android, you still need to create an Android Client ID with your SHA1, but this is only for verification - the token won’t be used (Android setup guide)
Code Changes
Import Changes
import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';import { SocialLogin } from '@capgo/capacitor-social-login';
Initialization
GoogleAuth.initialize({ clientId: 'CLIENT_ID.apps.googleusercontent.com', scopes: ['profile', 'email'], grantOfflineAccess: true,});
await SocialLogin.initialize({ google: { webClientId: 'WEB_CLIENT_ID.apps.googleusercontent.com', // Use Web Client ID for all platforms iOSClientId: 'IOS_CLIENT_ID', // for iOS mode: 'offline' // replaces grantOfflineAccess }});
Sign In
const user = await GoogleAuth.signIn();const res = await SocialLogin.login({ provider: 'google', options: { scopes: ['email', 'profile'], forceRefreshToken: true // if you need refresh token }});
Platform Specific Changes
Android
- Update your
MainActivity.java
(Full Android setup guide):
import ee.forgr.capacitor.social.login.GoogleProvider; import ee.forgr.capacitor.social.login.SocialLoginPlugin; import ee.forgr.capacitor.social.login.ModifiedMainActivityForSocialLoginPlugin; import com.getcapacitor.PluginHandle; import com.getcapacitor.Plugin; import android.content.Intent; import android.util.Log;
public class MainActivity extends BridgeActivity { public class MainActivity extends BridgeActivity implements ModifiedMainActivityForSocialLoginPlugin {
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);
if (requestCode >= GoogleProvider.REQUEST_AUTHORIZE_GOOGLE_MIN && requestCode < GoogleProvider.REQUEST_AUTHORIZE_GOOGLE_MAX) { PluginHandle pluginHandle = getBridge().getPlugin("SocialLogin"); if (pluginHandle == null) { Log.i("Google Activity Result", "SocialLogin login handle is null"); return; } Plugin plugin = pluginHandle.getInstance(); if (!(plugin instanceof SocialLoginPlugin)) { Log.i("Google Activity Result", "SocialLogin plugin instance is not SocialLoginPlugin"); return; } ((SocialLoginPlugin) plugin).handleGoogleLoginIntent(requestCode, data); } }
public void IHaveModifiedTheMainActivityForTheUseWithSocialLoginPlugin() {}}
iOS
-
No major changes needed in AppDelegate.swift (iOS setup guide)
-
Update your configuration in
capacitor.config.json
, we don’t use it in the new plugin:
{ "plugins": { "GoogleAuth": { "scopes": ["profile", "email"], "serverClientId": "xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com", "forceCodeForRefreshToken": true }}
Web
- Remove the Google Sign-In meta tags from your
index.html
if you were using them:
<meta name="google-signin-client_id" content="{your client id here}" /><meta name="google-signin-scope" content="profile email" />
Response Type Changes
The response object structure has changed. Here’s how to handle the new response:
interface GoogleLoginResponse { provider: 'google'; result: { accessToken: { token: string; expires: string; // ... other token fields } | null; idToken: string | null; profile: { email: string | null; familyName: string | null; givenName: string | null; id: string | null; name: string | null; imageUrl: string | null; }; };}
Additional Features
The new plugin offers additional social login providers:
Check the main documentation for setting up these additional providers.