Skip to content

Commit 3d92e3e

Browse files
authored
chore(supabase_flutter): Update the instructions to perform Google sign in (supabase#692)
chore: update the instructions to perform Google sign in
1 parent 6b7bad7 commit 3d92e3e

File tree

1 file changed

+31
-70
lines changed

1 file changed

+31
-70
lines changed

packages/supabase_flutter/README.md

Lines changed: 31 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -84,101 +84,62 @@ await supabase.auth.signInWithApple();
8484

8585
#### Native Google sign in
8686

87-
You can perform native Google sign in on Android and iOS using [flutter_appauth](https://pub.dev/packages/flutter_appauth).
87+
You can perform native Google sign in on Android and iOS using [google_sign_in](https://pub.dev/packages/google_sign_in).
88+
For platform specific settings, follow the instructions on README of the package.
8889

89-
First, you need to create a client ID in your Google Cloud console and add them to your Supabase dashboard in `Authentication -> Providers -> Google -> Authorized Client IDs`. You can add multiple client IDs as comma separated string.
90+
First, create client IDs for your app. You need to create a web client ID as well to perform Google sign-in with Supabase.
9091

92+
- [Steps to obtain web client ID](https://developers.google.com/identity/sign-in/android/start-integrating#configure_a_project)
9193
- [Steps to obtain Android client ID](https://developers.google.com/identity/sign-in/android/start-integrating#configure_a_project)
9294
- [Steps to obtain iOS client ID](https://developers.google.com/identity/sign-in/ios/start-integrating#get_an_oauth_client_id)
9395

94-
Second, add [flutter_appauth](https://pub.dev/packages/flutter_appauth) to your app and complete the [setup steps](https://pub.dev/packages/flutter_appauth#android-setup). For `appAuthRedirectScheme`, reverse DNS form of the client ID should be set (e.g. `com.googleusercontent.apps.*account_id*`). You also need [crypto](https://pub.dev/packages/crypto) package to hash nonce.
96+
Once you have registered your app and created the client IDs, add the web client ID in your Supabase dashboard in `Authentication -> Providers -> Google`. Also turn on the `Skip nonce check` option, which will enable Google sign-in on iOS.
9597

96-
```bash
97-
flutter pub add flutter_appauth crypto
98-
```
99-
100-
At this point you can perform native Google sign in using the following code. Make sure to replace the `clientId` and `applicationId` with your own.
98+
At this point you can perform native Google sign in using the following code. Be sure to replace the `webClientId` and `iosClientId` with your own.
10199

102100
```dart
103-
import 'dart:convert';
104-
import 'dart:math';
105-
import 'package:crypto/crypto.dart';
106-
import 'package:flutter_appauth/flutter_appauth.dart';
101+
import 'package:google_sign_in/google_sign_in.dart';
107102
import 'package:supabase_flutter/supabase_flutter.dart';
108103
109-
/// Function to generate a random 16 character string.
110-
String _generateRandomString() {
111-
final random = Random.secure();
112-
return base64Url.encode(List<int>.generate(16, (_) => random.nextInt(256)));
113-
}
104+
...
114105
115-
Future<AuthResponse> signInWithGoogle() {
116-
// Just a random string
117-
final rawNonce = _generateRandomString();
118-
final hashedNonce =
119-
sha256.convert(utf8.encode(rawNonce)).toString();
106+
Future<AuthResponse> _googleSignIn() async {
107+
/// TODO: update the Web client ID with your own.
108+
///
109+
/// Web Client ID that you registered with Google Cloud.
110+
const webClientId = 'my-web.apps.googleusercontent.com';
120111
121-
/// TODO: update the client ID with your own
112+
/// TODO: update the iOS client ID with your own.
122113
///
123-
/// Client ID that you registered with Google Cloud.
124-
/// You will have two different values for iOS and Android.
125-
const clientId = 'YOUR_CLIENT_ID_HERE';
126-
127-
/// reverse DNS form of the client ID + `:/` is set as the redirect URL
128-
final redirectUrl = '${clientId.split('.').reversed.join('.')}:/';
129-
130-
/// Fixed value for google login
131-
const discoveryUrl =
132-
'https://accounts.google.com/.well-known/openid-configuration';
133-
134-
final appAuth = FlutterAppAuth();
135-
136-
// authorize the user by opening the concent page
137-
final result = await appAuth.authorize(
138-
AuthorizationRequest(
139-
clientId,
140-
redirectUrl,
141-
discoveryUrl: discoveryUrl,
142-
nonce: hashedNonce,
143-
scopes: [
144-
'openid',
145-
'email',
146-
],
147-
),
148-
);
114+
/// iOS Client ID that you registered with Google Cloud.
115+
const iosClientId = 'my-ios.apps.googleusercontent.com';
149116
150-
if (result == null) {
151-
throw 'No result';
152-
}
117+
// Google sign in on Android will work without providing the Android
118+
// Client ID registered on Google Cloud.
153119
154-
// Request the access and id token to google
155-
final tokenResult = await appAuth.token(
156-
TokenRequest(
157-
clientId,
158-
redirectUrl,
159-
authorizationCode: result.authorizationCode,
160-
discoveryUrl: discoveryUrl,
161-
codeVerifier: result.codeVerifier,
162-
nonce: result.nonce,
163-
scopes: [
164-
'openid',
165-
'email',
166-
],
167-
),
120+
final GoogleSignIn googleSignIn = GoogleSignIn(
121+
clientId: iosClientId,
122+
serverClientId: webClientId,
168123
);
124+
final googleUser = await googleSignIn.signIn();
125+
final googleAuth = await googleUser!.authentication;
126+
final accessToken = googleAuth.accessToken;
127+
final idToken = googleAuth.idToken;
169128
170-
final idToken = tokenResult?.idToken;
171-
129+
if (accessToken == null) {
130+
throw 'No Access Token found.';
131+
}
172132
if (idToken == null) {
173-
throw 'No idToken';
133+
throw 'No ID Token found.';
174134
}
175135
176136
return supabase.auth.signInWithIdToken(
177137
provider: Provider.google,
178138
idToken: idToken,
179-
nonce: rawNonce,
139+
accessToken: accessToken,
180140
);
181141
}
142+
...
182143
```
183144

184145
### OAuth login

0 commit comments

Comments
 (0)