0

I'm creating an application that needs to store and export some information on google firestore. I have a google service account with the corresponding certificate and the exporter that was made with Node.js works as expected. On the other hand, I use flutter and the latest cloud_firestore plugin for the application that needs to store the information and I can not find any documentation about how to use google service account certificate with the Flutter plugin.

The cloud_firestore plugins do not seem to have any mechanism to handle authentication and the plugin firebase_auth does not seem to support google service accounts.

Was anyone able to use a Google service account with Firestore and Flutter?

Samet ÖZTOPRAK
  • 3,112
  • 3
  • 32
  • 33
jpuerto
  • 106
  • 7
  • Not sure if i understand correct, but you can use google login with flutter, is that what you want? – Tinus Jackson Jul 17 '19 at 14:08
  • I want to use a google account service but I've realised that it belongs to firebase-admin. See: https://firebase.google.com/docs/admin/setup I will have to found another way to authenticate the application. See this answer: https://stackoverflow.com/a/54293693/6852181 – jpuerto Jul 18 '19 at 15:28
  • Hey @jpuerto did you find any way to auth the app? Im looking for the same and can't find any post or documentation about this. – vtisnado Feb 27 '20 at 00:45

2 Answers2

0

Here is my answer if the comment is correct.

Here is an AuthService that i use.

This will handle the google signin.

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';

class AuthService {
  final GoogleSignIn _googleSignIn = GoogleSignIn();
  final FirebaseAuth _auth = FirebaseAuth.instance;

  Future<FirebaseUser> signIn(String email, String password) async {
    FirebaseUser u = await _auth.signInWithEmailAndPassword(
        email: email, password: password);
    updateUserData(u);
    return u;
  }

  Future<FirebaseUser> signInAnon() async {
    FirebaseUser user = await _auth.signInAnonymously();
    updateUserData(user);
    return user;
  }

  void signOut() {
    _auth.signOut();
  }

  Future<FirebaseUser> googleSignIn() async {
    try {
      GoogleSignInAccount googleSignInAccount = await _googleSignIn.signIn();
      GoogleSignInAuthentication googleAuth =
          await googleSignInAccount.authentication;

      final AuthCredential credential = GoogleAuthProvider.getCredential(
        accessToken: googleAuth.accessToken,
        idToken: googleAuth.idToken,
      );

      FirebaseUser user = await _auth.signInWithCredential(credential);
      updateUserData(user);

      return Future.value(user);
    } catch (error) {
      print(error);
      return Future.error(error);
    }
  }
}

In your pupspec you can import

  firebase_core: ^0.4.0
  firebase_auth: ^0.11.0
  cloud_firestore: ^0.11.0+1
  google_sign_in: ^4.0.1+3
Tinus Jackson
  • 3,397
  • 2
  • 25
  • 58
0

In your firebase project console you can add google service account in settings -> service accounts (settings/serviceaccounts/adminsdk). You also need to declare google as a provider in authentication -> providers At this point you need to import in your flutter project the new version of google-service.json file provided in the settings part of firebase console.

hawkbee
  • 1,442
  • 3
  • 18
  • 26