1
0
mirror of https://github.com/flutter/samples.git synced 2026-05-07 15:36:43 +00:00
Files
samples/experimental/web_dashboard/lib/src/auth/firebase.dart
John Ryan 4893a24625 Migrate web dashboard to null safety (#928)
* update dependencies

* Update to cloud_firestore 2.x.x

* run dart migrate

* Fix analyzer warnings from null safety migration

* Fix errors resulting from null safety migration

* Fix info level warnings

* run flutter format

* fix tests

* remove unused import, format
2021-10-12 14:38:34 -07:00

60 lines
1.5 KiB
Dart

// Copyright 2020, the Flutter project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:firebase_auth/firebase_auth.dart' hide User;
import 'package:flutter/services.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'auth.dart';
class FirebaseAuthService implements Auth {
final GoogleSignIn _googleSignIn = GoogleSignIn();
final FirebaseAuth _auth = FirebaseAuth.instance;
@override
Future<bool> get isSignedIn => _googleSignIn.isSignedIn();
@override
Future<User> signIn() async {
try {
return await _signIn();
} on PlatformException {
throw SignInException();
}
}
Future<User> _signIn() async {
GoogleSignInAccount? googleUser;
if (await isSignedIn) {
googleUser = await _googleSignIn.signInSilently();
} else {
googleUser = await _googleSignIn.signIn();
}
var googleAuth = await googleUser!.authentication;
var credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken, idToken: googleAuth.idToken);
var authResult = await _auth.signInWithCredential(credential);
return _FirebaseUser(authResult.user!.uid);
}
@override
Future<void> signOut() async {
await Future.wait([
_auth.signOut(),
_googleSignIn.signOut(),
]);
}
}
class _FirebaseUser implements User {
@override
final String uid;
_FirebaseUser(this.uid);
}