Skip to content

Commit 0b60faf

Browse files
fix: Update Google Sign In setup to use new Google Auth Platform (#393)
Co-authored-by: Marcelo Soares <marcelo.me.soares@gmail.com>
1 parent 15a49b7 commit 0b60faf

12 files changed

Lines changed: 394 additions & 116 deletions

File tree

docs/06-concepts/11-authentication/04-providers/02-google/01-setup.md

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Setup
22

3-
To set up Sign in with Google, you will need a Google account for your organization and set up a new project. For the project, you need to set up _Credentials_ and _OAuth consent screen_. You will also need to add the `serverpod_auth_idp_flutter` package to your app and do some additional setup depending on each platform.
3+
To set up Sign in with Google, you will need a Google account for your organization and set up a new project. You will also need to add the `serverpod_auth_idp_flutter` package to your app and do some additional setup depending on each platform.
44

55
:::note
66
Right now, we have official support for iOS, Android, and Web for Google Sign In.
@@ -20,28 +20,54 @@ To be allowed to access user data and use the authentication method in Serverpod
2020

2121
[Enable it here](https://console.cloud.google.com/apis/library/people.googleapis.com) or find it yourself by navigating to the _Library_ section under _APIs & Services_. Search for _Google People API_, select it, and click on _Enable_.
2222

23-
### Setup OAuth consent screen
23+
### Configure Google Auth Platform
2424

25-
The setup for the OAuth consent screen can be found [here](https://console.cloud.google.com/apis/credentials/consent) or under _APIs & Services_ > _OAuth consent screen_.
25+
If you haven't already, enable the Google Auth Platform for your project. Navigate to the [Google Auth Platform overview](https://console.cloud.google.com/auth/overview) and click _Get started_.
2626

27-
1. Fill in all the required information, for production use you need a domain that adds under `Authorized` domains.
27+
![Google Auth Platform Overview](/img/authentication/providers/google/4-auth-platform-overview.png)
2828

29-
2. Add the scopes `.../auth/userinfo.email` and `.../auth/userinfo.profile`.
29+
Configure the following settings in the Google Auth Platform:
3030

31-
3. Add your email to the test users so that you can test your integration in development mode.
31+
1. **Data Access**: Navigate to the [Data Access](https://console.cloud.google.com/auth/scopes) page to add the required scopes. Add the scopes `.../auth/userinfo.email` and `.../auth/userinfo.profile`.
3232

3333
![Scopes](/img/authentication/providers/google/1-scopes.png)
3434

35+
:::tip
36+
If you need access to additional Google APIs (e.g., Calendar, Drive), you can add more scopes here. See [Accessing Google APIs](./configuration#accessing-google-apis) for details on requesting additional scopes and using them with the `getExtraGoogleInfoCallback` on the server.
37+
:::
38+
39+
1. **Audience**: Navigate to the [Audience](https://console.cloud.google.com/auth/audience) page to add test users. Add your email so you can test your integration in development mode.
40+
41+
:::tip
42+
For production apps, you can configure additional branding options on the [Branding](https://console.cloud.google.com/auth/branding) page. See the [Google Auth Platform documentation](https://developers.google.com/identity/protocols/oauth2) for more details.
43+
:::
44+
3545
## Server-side configuration
3646

37-
Create the server credentials in the Google Cloud Console. Navigate to _Credentials_ under _APIs & Services_. Click _Create Credentials_ and select _OAuth client ID_. Configure the OAuth client as a _**Web application**_. If you have a domain add it to the `Authorized JavaScript origins` and `Authorized redirect URIs`. For development purposes we can add `http://localhost:8082` to both fields, this is the address to the web server.
47+
Create the server credentials in the Google Auth Platform. Navigate to _Clients_ and click _Create Client_. Configure the OAuth client as a _**Web application**_. If you have a domain add it to the `Authorized JavaScript origins` and `Authorized redirect URIs`. For development purposes, we can add `http://localhost:8082` to both fields, which is the address to the web server.
3848

39-
![Google credentials](/img/6-google-credentials.jpg)
49+
![Google credentials](/img/authentication/providers/google/5-clients.png)
4050

4151
Download the JSON file for your web application OAuth client. This file contains both the client id and the client secret. You will need to supply the contents of the file to the `clientSecret` property of the `GoogleIdpConfig` object.
4252

4353
This can be done by pasting the contents of the JSON file into the `googleClientSecret` key in the `config/passwords.yaml` file or setting as value of the `SERVERPOD_PASSWORD_googleClientSecret` environment variable. Alternatively, you can read the file contents directly using the `GoogleClientSecret.fromJsonFile()` method.
4454

55+
```yaml
56+
development:
57+
googleClientSecret: |
58+
{
59+
"web": {
60+
"client_id": "your-client-id.apps.googleusercontent.com",
61+
"project_id": "your-project-id",
62+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
63+
"token_uri": "https://oauth2.googleapis.com/token",
64+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
65+
"client_secret": "your-client-secret",
66+
"redirect_uris": ["http://localhost:8082"]
67+
}
68+
}
69+
```
70+
4571
In your main `server.dart` file, configure the Google identity provider:
4672

4773
```dart
@@ -103,27 +129,27 @@ For our client-side configurations, we have to first create client-side credenti
103129

104130
### iOS
105131

106-
Create the client credentials. Navigate to _Credentials_ under _APIs & Services_. Click _Create Credentials_ and select _OAuth client ID_. Configure the OAuth client as Application type _**iOS**_.
132+
Create the client credentials in the Google Auth Platform. Navigate to _Clients_ and click _Create Client_. Configure the OAuth client as Application type _**iOS**_.
107133

108-
Fill in all the required information, and create the credentials. Then download the `plist` file rename it to `GoogleService-Info.plist` and put it inside your ios project folder. Then drag and drop it into your XCode project to include the file in your build.
134+
Fill in all the required information and create the credentials. Download the `plist` file - you'll need to extract values from it to configure your app.
109135

110-
Open the `GoogleService-Info.plist` in your editor and add the SERVER_CLIENT_ID if it does not exist:
136+
Open your `ios/Runner/Info.plist` file and add the following keys:
111137

112138
```xml
113139
<dict>
114140
...
115-
<key>SERVER_CLIENT_ID</key>
141+
<key>GIDClientID</key>
142+
<string>your_ios_client_id</string>
143+
<key>GIDServerClientID</key>
116144
<string>your_server_client_id</string>
117145
</dict>
118146
```
119147

120-
Replace `your_server_client_id` with the client id from the JSON file you put inside the config folder in the server.
148+
Replace `your_ios_client_id` with the `CLIENT_ID` value from the downloaded plist file, and `your_server_client_id` with the client ID from the server credentials JSON file.
121149

122150
#### Add the URL scheme
123151

124-
To allow us to navigate back to the app after the user has signed in we have to add the URL Scheme, the scheme is the reversed client ID of your iOS app. You can find it inside the `GoogleService-Info.plist` file.
125-
126-
Open the `info.plist` file in your editor and add the following to register the URL Scheme.
152+
To allow navigation back to the app after sign-in, add the URL scheme to your `Info.plist`. The scheme is the reversed client ID of your iOS app (found as `REVERSED_CLIENT_ID` in the downloaded plist file).
127153

128154
```xml
129155
<dict>
@@ -135,22 +161,22 @@ Open the `info.plist` file in your editor and add the following to register the
135161
<string>Editor</string>
136162
<key>CFBundleURLSchemes</key>
137163
<array>
138-
<string>your_reversed_client_id</string>
164+
<string>com.googleusercontent.apps.your_client_id</string>
139165
</array>
140166
</dict>
141167
</array>
142168
</dict>
143169
```
144170

145-
Replace `your_reversed_client_id` with your reversed client ID.
171+
Replace the URL scheme with your actual reversed client ID.
146172

147173
:::info
148174
If you have any social logins in your app you also need to integrate "Sign in with Apple" to publish your app to the app store. ([Read more](https://developer.apple.com/sign-in-with-apple/get-started/)).
149175
:::
150176

151177
### Android
152178

153-
Create the client credentials. Navigate to _Credentials_ under _APIs & Services_. Click _Create Credentials_ and select _OAuth client ID_. Configure the OAuth client as Application type _**Android**_.
179+
Create the client credentials in the Google Auth Platform. Navigate to _Clients_ and click _Create Client_. Configure the OAuth client as Application type _**Android**_.
154180

155181
Fill in all required information, you can get the debug SHA-1 hash by running `./gradlew signingReport` in your Android project directory. Create the credentials and download the JSON file.
156182

@@ -164,11 +190,15 @@ $ keytool -list -v -keystore /path/to/keystore
164190
```
165191
:::
166192

193+
:::tip
194+
If you encounter issues with Google Sign-In on Android, check the [official troubleshooting guide](https://pub.dev/packages/google_sign_in_android#troubleshooting) for common solutions.
195+
:::
196+
167197
### Web
168198

169-
There is no need to create any client credentials for the web, since it uses the same client ID as the server. However, you have to modify the server credentials inside the Google Cloud Console.
199+
There is no need to create any client credentials for the web, since it uses the same client ID as the server. However, you have to modify the server credentials in the Google Auth Platform.
170200

171-
Navigate to _Credentials_ under _APIs & Services_ and select the server credentials. Under `Authorized JavaScript origins` and `Authorized redirect URIs` add the domain for your Flutter app, for development, this is `http://localhost:<port>` where the port is the port you are using.
201+
Navigate to _Clients_ and select the server credentials (the one configured as a _**Web application**_). Under `Authorized JavaScript origins` and `Authorized redirect URIs` add the domain for your Flutter app, for development, this is `http://localhost:<port>` where the port is the port you are using.
172202

173203
:::info
174204
Force flutter to run on a specific port by running.
@@ -178,11 +208,7 @@ $ flutter run -d chrome --web-port=49660
178208
```
179209
:::
180210

181-
Set up the actual redirect URI where the user will navigate after the sign-in. You can choose any path you want but it has to be the same in the credentials, your server, and Flutter configurations.
182-
183-
For example, using the path `/googlesignin`.
184-
185-
For development inside `Authorized redirect URIs` add `http://localhost:8082/googlesignin`, in production use `https://example.com/googlesignin`.
211+
Set up the redirect URI where the user will navigate after sign-in. For development, add `http://localhost:8082` inside `Authorized redirect URIs`. In production, use your server's domain (e.g., `https://example.com`).
186212

187213
Then, on the `web/index.html` file, add the following to the `<head>` section:
188214

docs/06-concepts/11-authentication/04-providers/02-google/02-configuration.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ final googleIdpConfig = GoogleIdpConfigFromPasswords(
6767

6868
The default setup allows access to basic user information, such as email, profile image, and name. You may require additional access scopes, such as accessing a user's calendar, contacts, or files. To do this, you will need to:
6969

70-
- Add the required scopes to the OAuth consent screen.
70+
- Add the required scopes to the [Data Access](./setup#configure-google-auth-platform) page in the Google Auth Platform.
7171
- Request access to the scopes when signing in. Do this by setting the `scopes` parameter of the `GoogleSignInWidget` or `GoogleAuthController`.
7272

7373
A full list of available scopes can be found [here](https://developers.google.com/identity/protocols/oauth2/scopes).
@@ -122,3 +122,48 @@ If lightweight sign-in fails (e.g., no previous session exists or the user dismi
122122
:::note
123123
The lightweight sign-in attempt happens automatically when the controller is initialized, typically at app launch. If successful, users will be signed in without any additional interaction.
124124
:::
125+
126+
### Configuring Client IDs on the App
127+
128+
If no client IDs are provided programmatically, the underlying `google_sign_in` package falls back to reading from platform-specific configuration files (e.g., `GoogleService-Info.plist` for iOS, `google-services.json` for Android). To set them programmatically, you can use the following methods.
129+
130+
#### Passing Client IDs in Code
131+
132+
You can pass the client IDs directly when initializing the Google Sign-In service:
133+
134+
```dart
135+
client.auth.initializeGoogleSignIn(
136+
clientId: '<platform_client_id>.apps.googleusercontent.com',
137+
serverClientId: '<web_client_id>.apps.googleusercontent.com',
138+
);
139+
```
140+
141+
This approach is useful when you need different client IDs per platform and want to manage them in your Dart code.
142+
143+
#### Using Environment Variables
144+
145+
Alternatively, you can pass client IDs during build time using the `--dart-define` option. The Google Sign-In provider supports the following environment variables:
146+
147+
- `GOOGLE_CLIENT_ID`: The platform-specific OAuth client ID
148+
- `GOOGLE_SERVER_CLIENT_ID`: The server (web application) OAuth client ID
149+
150+
If `clientId` and `serverClientId` values are not supplied when initializing the service, the provider will automatically fetch them from these environment variables.
151+
152+
**Example usage:**
153+
154+
```bash
155+
flutter run \
156+
-d "<device>" \
157+
--dart-define="GOOGLE_CLIENT_ID=<platform_client_id>.apps.googleusercontent.com" \
158+
--dart-define="GOOGLE_SERVER_CLIENT_ID=<web_client_id>.apps.googleusercontent.com"
159+
```
160+
161+
This approach is useful when you need to:
162+
163+
- Manage separate client IDs for different platforms (Android, iOS, Web) in a centralized way
164+
- Avoid committing client IDs to version control
165+
- Configure different credentials for different build environments (development, staging, production)
166+
167+
:::tip
168+
You can also set these environment variables in your IDE's run configuration or CI/CD pipeline to avoid passing them manually each time.
169+
:::
-5.64 KB
Loading
-148 KB
Loading
155 KB
Loading
133 KB
Loading

0 commit comments

Comments
 (0)