google_sign_in_dartio
A Flutter package that implements Google Sign In
in pure Dart. This package is compatible with google_sign_in plugin
and works on all platforms Flutter supports but it's intended to be
mainly used on Desktop.
Getting Started
Install and initialization
- Depend on it
dependencies: google_sign_in: ^6.2.0 google_sign_in_dartio: ^0.3.2 - Run
flutter pub get - Import
import 'package:google_sign_in/google_sign_in.dart'; import 'package:google_sign_in_dartio/google_sign_in_dartio.dart'; - Register the package
Note: You might want toimport 'package:flutter/material.dart'; import 'package:google_sign_in/google_sign_in.dart'; import 'package:google_sign_in_dartio/google_sign_in_dartio.dart'; void main() { if (isDesktop) { GoogleSignInDart.register(clientId: <clientId>); } runApp(MyApp()); }awaitfor theregistermethod to finish before calling anyGoogleSignInmethods when your app starts.
Usage
You can use the normal GoogleSignIn methods.
final GoogleSignInAccount account = await GoogleSignIn().signIn();
Set up the OAuth client
The sign-in opens the browser and receives Google's answer on a local server at
http://127.0.0.1:<port> (the loopback IP address flow). Only one OAuth client type allows
that redirect:
- In the Google Cloud console, open your project, then APIs & Services -> Credentials.
- Configure the OAuth consent screen if the console asks for it.
- Create credentials -> OAuth client ID, application type Desktop app (called "Other" in older consoles).
- Pass its client ID to
GoogleSignInDart.register(clientId: ...). The plugin runs no setup of its own, so callregisterbefore the firstGoogleSignIncall, on every platform where you use this package.
A Desktop app client has no list of redirect URIs: you register none, and the local server
picks a free port each time (or the port you pass to register). Google's guide for
desktop apps
uses this flow for macOS, Linux and Windows.
Do not use the client ID of an Android, iOS or Chrome app client. Google
blocked the loopback flow
for those client types in 2022, and the browser then shows Error 400: invalid_request. A Web
application client needs each exact redirect URI registered, port included; use a Desktop app
client instead.
Provide a code exchange endpoint
The user accessToken expires after about one hour, after which you need to ask the user to
login again. To keep the user logged in you need a refresh token, and getting one needs the
client secret, which an app cannot keep secret. So the package can hand the authorization code
to a server of yours: the exchangeEndpoint.
The endpoint is your own HTTPS endpoint (for example a Cloud Function). It holds the client
secret, calls Google's token endpoint (https://oauth2.googleapis.com/token) for the code
exchange and for each refresh, and returns Google's JSON answer. The package sends it JSON
(code, codeVerifier, clientId and redirectUrl, or refreshToken and clientId), not
the form fields Google's token endpoint expects, so pointing exchangeEndpoint at Google's
token URL fails with invalid_grant. The example endpoint
documents both requests and has a Cloud Function you can deploy.
When Google refuses a request, the endpoint answers with a 400, a 401 or a 403 and Google's
error JSON as it is ({"error": "invalid_grant", ...}). The package reads only the error
code of such an answer; an error in any other form (plain text, another status) is a failure
without a code.
The package trusts the TLS connection to your endpoint and to Google. Do not install a global
HttpOverrides that accepts any certificate (a badCertificateCallback that returns true),
not even for another host: it removes that check for the sign-in as well.
Without an exchangeEndpoint the package gets the tokens straight from the browser, and the
user signs in again when the access token expires.
NOTE:
If you use this only for FirebaseAuth you don't need the code exchange since the token must be valid only when you
use the signInWithCredentials method.
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:google_sign_in_dartio/google_sign_in_dartio.dart';
void main() {
if (isDesktop) {
GoogleSignInDart.register(
clientId: <clientId>,
exchangeEndpoint: <endpoint>,
);
}
runApp(MyApp());
}
NOTE:
GoogleSignInTokenData exposes serverAuthCode field that should
contain the exchange code from the authorization request. This will
always be null when using this package because we already allow you to
provide a code exchange endpoint witch exposes the code and code
verifier in a trusted environment and encourages not to do the code
exchange on the client.
See instruction on how to deploy a code exchange endpoint.
Setup checklist
- A Desktop app OAuth client in the Google Cloud project, and its client ID.
GoogleSignInDart.register(clientId: ...)awaited before the firstGoogleSignIncall.- On macOS, the
com.apple.security.network.serverandcom.apple.security.network.cliententitlements, so that the app may run the local server and call Google. - To keep users signed in for more than an hour: your own exchange endpoint, holding the
client secret, passed as
exchangeEndpoint. signOutremoves only this package's values from the shared preferences (keys starting withDataStorage___). A customStoreshould do the same inclearAll.