Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'dart:io';
- import 'package:flutter/material.dart';
- import 'package:firebase_auth/firebase_auth.dart';
- import 'package:google_sign_in/google_sign_in.dart';
- final FirebaseAuth _auth = FirebaseAuth.instance;
- final GoogleSignIn _googleSignIn = new GoogleSignIn();
- void main() {
- runApp(new MyApp());
- }
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return new MaterialApp(
- title: 'Firebase Auth Demo',
- home: new MyHomePage(title: 'Firebase Auth Demo'),
- );
- }
- }
- class MyHomePage extends StatefulWidget {
- MyHomePage({Key key, this.title}) : super(key: key);
- final String title;
- @override
- _MyHomePageState createState() => new _MyHomePageState();
- }
- class _MyHomePageState extends State<MyHomePage> {
- Future<String> _message = new Future<String>.value('');
- Future<String> _testSignInAnonymously() async {
- final FirebaseUser user = await _auth.signInAnonymously();
- assert(user != null);
- assert(user == _auth.currentUser);
- assert(user.isAnonymous);
- assert(!user.isEmailVerified);
- assert(await user.getToken() != null);
- if (Platform.isIOS) {
- // Anonymous auth doesn't show up as a provider on iOS
- assert(user.providerData.isEmpty);
- } else if (Platform.isAndroid) {
- // Anonymous auth does show up as a provider on Android
- assert(user.providerData.length == 1);
- assert(user.providerData[0].providerId == 'firebase');
- assert(user.providerData[0].uid != null);
- assert(user.providerData[0].displayName == null);
- assert(user.providerData[0].photoUrl == null);
- assert(user.providerData[0].email == null);
- }
- return 'signInAnonymously succeeded: $user';
- }
- Future<String> _testSignInWithGoogle() async {
- final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
- final GoogleSignInAuthentication googleAuth =
- await googleUser.authentication;
- final FirebaseUser user = await _auth.signInWithGoogle(
- accessToken: googleAuth.accessToken,
- idToken: googleAuth.idToken,
- );
- assert(user.email != null);
- assert(user.displayName != null);
- assert(!user.isAnonymous);
- assert(await user.getToken() != null);
- return 'signInWithGoogle succeeded: $user';
- }
- @override
- Widget build(BuildContext context) {
- return new Scaffold(
- appBar: new AppBar(
- title: new Text(widget.title),
- ),
- body: new Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- new MaterialButton(
- child: const Text('Test signInAnonymously'),
- onPressed: () {
- setState(() {
- _message = _testSignInAnonymously();
- });
- }),
- new MaterialButton(
- child: const Text('Test signInWithGoogle'),
- onPressed: () {
- setState(() {
- _message = _testSignInWithGoogle();
- });
- }),
- new FutureBuilder<String>(
- future: _message,
- builder: (_, AsyncSnapshot<String> snapshot) {
- return new Text(snapshot.data ?? '',
- style: const TextStyle(
- color: const Color.fromARGB(255, 0, 155, 0)));
- }),
- ],
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement