1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-12 07:48:55 +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

@@ -6,7 +6,7 @@ import 'package:flutter/material.dart';
import '../auth/auth.dart';
class SignInPage extends StatefulWidget {
class SignInPage extends StatelessWidget {
final Auth auth;
final ValueChanged<User> onSuccess;
@@ -15,31 +15,89 @@ class SignInPage extends StatefulWidget {
@required this.onSuccess,
});
@override
_SignInPageState createState() => _SignInPageState();
}
class _SignInPageState extends State<SignInPage> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
child: Text('Sign In'),
onPressed: () async {
var user = await widget.auth.signIn();
if (user != null) {
widget.onSuccess(user);
} else {
throw ('Unable to sign in');
}
},
),
child: SignInButton(auth: auth, onSuccess: onSuccess),
),
);
}
}
class SignInButton extends StatefulWidget {
final Auth auth;
final ValueChanged<User> onSuccess;
SignInButton({
@required this.auth,
@required this.onSuccess,
});
@override
_SignInButtonState createState() => _SignInButtonState();
}
class _SignInButtonState extends State<SignInButton> {
Future<bool> _checkSignInFuture;
@override
void initState() {
super.initState();
_checkSignInFuture = _checkIfSignedIn();
}
// Check if the user is signed in. If the user is already signed in (for
// example, if they signed in and refreshed the page), invoke the `onSuccess`
// callback right away.
Future<bool> _checkIfSignedIn() async {
var alreadySignedIn = await widget.auth.isSignedIn;
if (alreadySignedIn) {
var user = await widget.auth.signIn();
widget.onSuccess(user);
}
return alreadySignedIn;
}
Future<void> _signIn() async {
try {
var user = await widget.auth.signIn();
widget.onSuccess(user);
} on SignInException {
_showError();
}
}
@override
Widget build(BuildContext context) {
return FutureBuilder<bool>(
future: _checkSignInFuture,
builder: (context, snapshot) {
// If signed in, or the future is incomplete, show a circular
// progress indicator.
var alreadySignedIn = snapshot.data;
if (snapshot.connectionState != ConnectionState.done ||
alreadySignedIn == true) {
return CircularProgressIndicator();
}
// If sign in failed, show toast and the login button
if (snapshot.hasError) {
_showError();
}
return RaisedButton(
child: Text('Sign In with Google'),
onPressed: () => _signIn(),
);
},
);
}
void _showError() {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text('Unable to sign in.'),
),
);
}