Flutter Web: Google Sign In
Tags: flutter, flutterweb, google, auth
Published: 2020-04-28 06:12:52

{% youtube E8kgHqEz6fs %}

Flutter is still gaining traction and people are learning and exploring the wonders of Flutter. Flutter web is in the same stance. And now, you might want to have some authentication to be inserted in you flutter web or you either have an auth service and want to see if it's compatible to the web.

1 . Install google sign in in your pubspec.yaml file. The web implementation of google sign is in the official package.

```dart
// pubspec.yaml

google_sign_in: <latest version>
```

2 . Then, you have to go to the your index.html file and add your google sign in meta tag:

```html
<meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com">
```

3 . To get your client id, you have to your credentials page in your google api developer console through this website: https://console.developers.google.com/apis/credentials.

4 . Under your OAuth 2.0 Client IDs, if you have the Web Client api, just copy the client id and paste in your index.html.

![image](https://user-images.githubusercontent.com/31005114/80448924-5b472680-8950-11ea-8fba-373bb8d7e437.png)

```html
<meta name="google-signin-client_id" content="afda89df7b328sadf.apps.googleusercontent.com">
```

4 .a. If you do not have the Web Client api, make sure you configure your OAuth screen. 

![image](https://user-images.githubusercontent.com/31005114/80449184-06f07680-8951-11ea-9589-cd562970e07d.png)

To configure, and you don't have G-Suite, then you can only make it external.

![image](https://user-images.githubusercontent.com/31005114/80449473-d0ffc200-8951-11ea-830f-7f6eb7d0a5eb.png)

Once you're in, you have to update the name of the project and save it

![image](https://user-images.githubusercontent.com/31005114/80449302-5afb5b00-8951-11ea-8ca5-04e64d43f76a.png)

4 .b. Then create the OAuth credential web client. 

![image](https://user-images.githubusercontent.com/31005114/80449611-2fc53b80-8952-11ea-98b1-044ad31f97b3.png)

Choose Web application. Don't need to rename the default name

![image](https://user-images.githubusercontent.com/31005114/80449677-52575480-8952-11ea-8a5c-6a50f162ed81.png)

And set the authorised Javascript origins to http://localhost:5000 for debugging.

![image](https://user-images.githubusercontent.com/31005114/80449760-8763a700-8952-11ea-9036-c1c9ec170d44.png)

Copy the client id and paste in your index.html.

![image](https://user-images.githubusercontent.com/31005114/80449921-e9241100-8952-11ea-8695-841adb92085c.png)

5 . Go in the settings of the Web Client and make sure your has authorised Javascript origins to http://localhost:5000 for debugging.

![image](https://user-images.githubusercontent.com/31005114/80450189-9139da00-8953-11ea-9388-5b673c989e8e.png)

6 . Instruction Source from: [google_sign_in_web package](https://pub.dev/packages/google_sign_in_web) 

Normally `flutter run` starts in a random port. In the case where you need to deal with authentication like the above, that's not the most appropriate behavior.

You can tell `flutter run` to listen for requests in a specific host and port with the following:

```
flutter run -d chrome --web-hostname localhost --web-port 5000
```

You can set your launch.json in your VSCode:

```json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Flutter with Web Port 5000",
      "request": "launch",
      "type": "dart",
      "args": ["--web-port", "5000"]
    }
  ]
}
```

7 . Add the import:

```dart
import 'package:google_sign_in/google_sign_in.dart';
```

8 . Initialize GoogleSignIn with the scopes you want:

```dart
GoogleSignIn _googleSignIn = GoogleSignIn(
  scopes: [
    'email',
  ],
);
```

9 . You can now use the `GoogleSignIn` class to authenticate in your Dart code, e.g.

```dart
Future<void> _handleSignIn() async {
  try {
    await _googleSignIn.signIn();
  } catch (error) {
    print(error);
  }
}
```