authyra_google signs users in with the native Google SDK (google_sign_in 7): Credential Manager on Android, the Google SDK on iOS and macOS, Google Identity Services on the web.
flutter pub add authyra_google
With your backend (recommended)
Google's ID token proves who the user is. Your backend verifies it and returns your session tokens.
GoogleProvider.withExchange(
serverClientId: 'xxxx.apps.googleusercontent.com', // Your "Web application" client id.
exchange: (credential) async {
final response = await authApi.post<Map<String, dynamic>>(
'/auth/google',
data: {'id_token': credential.idToken},
);
final user = response.data!['user'] as Map<String, dynamic>;
return AuthSignInResult(
user: AuthUser(id: user['id'] as String, email: credential.email),
tokens: tokensFrom(response.data!),
);
},
refresh: refreshWithApi, // Your refresh endpoint, as in CredentialsProvider.
signOut: signOutWithApi, // Optional: revoke on your backend.
)
await authyra.signIn('google');
Return null from exchange to reject the sign-in. An AuthException thrown there (for example AuthDeniedException) reaches the caller unchanged; any other error becomes a SignInFailedException.
refresh and signOut talk to your backend. Google's own tokens are never stored as session tokens and never refreshed from the app.
What your backend checks
The GoogleCredential carries idToken, userId, email, displayName and photoUrl. Trust only the ID token, after verifying:
- its signature against Google's public keys,
aud: one of your client ids,iss:accounts.google.comorhttps://accounts.google.com,exp: not expired.
Google's server libraries do this in one call.
Identity only
GoogleProvider(serverClientId: 'xxxx.apps.googleusercontent.com')
The session has no tokens; the ID token is in session.providerData['idToken'], valid for one hour. Use this to hand the credential to Firebase or Supabase, or when you only need to know who the user is.
Setup
Follow the platform setup of google_sign_in. Then:
| Platform | Notes |
|---|---|
| Android | An ID token is only issued when serverClientId (a client of type "Web application") is set. |
| iOS, macOS | Add the iOS client id and its reversed URL scheme, as google_sign_in describes. |
| Web | authenticate() does not exist on the web: render Google's button and call signIn when the screen opens. It completes when the user uses the button. |
On the web, add google_sign_in_web to your app's dependencies, then:
import 'package:google_sign_in_web/web_only.dart' as google_web;
// When the sign-in screen opens:
final signingIn = authyra.signIn('google');
// In its build method:
google_web.renderButton();
The SDK is initialized once, on first use, with clientId, serverClientId and hostedDomain. If your app also calls google_sign_in directly, initialize it yourself and pass initializeSdk: false.
Errors
| Situation | Exception |
|---|---|
| The user closes the picker | SignInCancelledException |
| Misconfiguration, missing ID token, network | SignInFailedException, with a message naming the fix when possible |