1
0
mirror of https://github.com/flutter/samples.git synced 2026-04-03 02:02:27 +00:00

[web_dashboard] add logout (#447)

* logout wip

* Use AnimatedSwitcher, change lingo from "login" to signIn"

* add automatic sign-in

* fix flashing sign in button

* sign out of FirebaseAuth and GoogleSignIn

* formatting

* change isSignedIn() to getter

* Add error handling for sign in

* improve error handling at login screen
This commit is contained in:
John Ryan
2020-06-01 14:39:06 -07:00
committed by GitHub
parent 46a3f2dd09
commit 8a9bcfa113
7 changed files with 218 additions and 55 deletions

View File

@@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.
abstract class Auth {
Future<bool> get isSignedIn;
Future<User> signIn();
Future signOut();
}
@@ -10,3 +11,5 @@ abstract class Auth {
abstract class User {
String get uid;
}
class SignInException implements Exception {}

View File

@@ -2,6 +2,7 @@
// 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:flutter/services.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:firebase_auth/firebase_auth.dart' hide FirebaseUser;
@@ -11,9 +12,20 @@ class FirebaseAuthService implements Auth {
final GoogleSignIn _googleSignIn = GoogleSignIn();
final FirebaseAuth _auth = FirebaseAuth.instance;
Future<bool> get isSignedIn => _googleSignIn.isSignedIn();
Future<User> signIn() async {
try {
return await _signIn();
} on PlatformException catch (e) {
print('Unable to sign in: $e');
throw SignInException();
}
}
Future<User> _signIn() async {
GoogleSignInAccount googleUser;
if (await _googleSignIn.isSignedIn()) {
if (await isSignedIn) {
googleUser = await _googleSignIn.signInSilently();
} else {
googleUser = await _googleSignIn.signIn();
@@ -30,7 +42,10 @@ class FirebaseAuthService implements Auth {
}
Future<void> signOut() async {
await _auth.signOut();
await Future.wait([
_auth.signOut(),
_googleSignIn.signOut(),
]);
}
}
@@ -39,3 +54,4 @@ class _FirebaseUser implements User {
_FirebaseUser(this.uid);
}

View File

@@ -2,11 +2,20 @@
// 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 'dart:math';
import 'auth.dart';
class MockAuthService implements Auth {
Future<bool> get isSignedIn async => false;
@override
Future<User> signIn() async {
// Sign in will randomly fail 25% of the time.
var random = Random();
if (random.nextInt(4) == 0) {
throw SignInException();
}
return MockUser();
}